How to include an EJS file in app.js file?










0















I have an index.ejs file in which I have used script tag and written some javascript for dom manipulation. Now I want to use a variable from this file to my app.js file. How to achieve that?
For referring the variable of ejs file we do like this:



someVariable:value


I just want to do vice versa, i.e. access variable declared in script tag of ejs file to app.js file.



//index.ejs



<script>
window.onload=function()var c = document.getElementById("city_search").options[1].value;
console.log(c);
;
function changeCarousel()
var x = document.getElementById("city_search").value;
console.log(x);

</script>


How do I access variable x in app.js, since it is not declared there.










share|improve this question
























  • EJS is one-way. You render the template in app.js based on variables there, and it gets delivered to the browser. There's no mechanism for the browser to return values back. You would need to add an api for that.

    – Jim B.
    Nov 11 '18 at 22:34











  • @JimB. is correct, you would need to create an API endpoint for posting the data back to the server, and then doing something with it. The other way you could do it would be with something like websockets between the client and the server.

    – Joshua Terrill
    Nov 12 '18 at 0:31















0















I have an index.ejs file in which I have used script tag and written some javascript for dom manipulation. Now I want to use a variable from this file to my app.js file. How to achieve that?
For referring the variable of ejs file we do like this:



someVariable:value


I just want to do vice versa, i.e. access variable declared in script tag of ejs file to app.js file.



//index.ejs



<script>
window.onload=function()var c = document.getElementById("city_search").options[1].value;
console.log(c);
;
function changeCarousel()
var x = document.getElementById("city_search").value;
console.log(x);

</script>


How do I access variable x in app.js, since it is not declared there.










share|improve this question
























  • EJS is one-way. You render the template in app.js based on variables there, and it gets delivered to the browser. There's no mechanism for the browser to return values back. You would need to add an api for that.

    – Jim B.
    Nov 11 '18 at 22:34











  • @JimB. is correct, you would need to create an API endpoint for posting the data back to the server, and then doing something with it. The other way you could do it would be with something like websockets between the client and the server.

    – Joshua Terrill
    Nov 12 '18 at 0:31













0












0








0








I have an index.ejs file in which I have used script tag and written some javascript for dom manipulation. Now I want to use a variable from this file to my app.js file. How to achieve that?
For referring the variable of ejs file we do like this:



someVariable:value


I just want to do vice versa, i.e. access variable declared in script tag of ejs file to app.js file.



//index.ejs



<script>
window.onload=function()var c = document.getElementById("city_search").options[1].value;
console.log(c);
;
function changeCarousel()
var x = document.getElementById("city_search").value;
console.log(x);

</script>


How do I access variable x in app.js, since it is not declared there.










share|improve this question
















I have an index.ejs file in which I have used script tag and written some javascript for dom manipulation. Now I want to use a variable from this file to my app.js file. How to achieve that?
For referring the variable of ejs file we do like this:



someVariable:value


I just want to do vice versa, i.e. access variable declared in script tag of ejs file to app.js file.



//index.ejs



<script>
window.onload=function()var c = document.getElementById("city_search").options[1].value;
console.log(c);
;
function changeCarousel()
var x = document.getElementById("city_search").value;
console.log(x);

</script>


How do I access variable x in app.js, since it is not declared there.







javascript node.js express ejs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '18 at 21:17









xor

1,8721224




1,8721224










asked Nov 11 '18 at 21:13









ApoorvApoorv

83




83












  • EJS is one-way. You render the template in app.js based on variables there, and it gets delivered to the browser. There's no mechanism for the browser to return values back. You would need to add an api for that.

    – Jim B.
    Nov 11 '18 at 22:34











  • @JimB. is correct, you would need to create an API endpoint for posting the data back to the server, and then doing something with it. The other way you could do it would be with something like websockets between the client and the server.

    – Joshua Terrill
    Nov 12 '18 at 0:31

















  • EJS is one-way. You render the template in app.js based on variables there, and it gets delivered to the browser. There's no mechanism for the browser to return values back. You would need to add an api for that.

    – Jim B.
    Nov 11 '18 at 22:34











  • @JimB. is correct, you would need to create an API endpoint for posting the data back to the server, and then doing something with it. The other way you could do it would be with something like websockets between the client and the server.

    – Joshua Terrill
    Nov 12 '18 at 0:31
















EJS is one-way. You render the template in app.js based on variables there, and it gets delivered to the browser. There's no mechanism for the browser to return values back. You would need to add an api for that.

– Jim B.
Nov 11 '18 at 22:34





EJS is one-way. You render the template in app.js based on variables there, and it gets delivered to the browser. There's no mechanism for the browser to return values back. You would need to add an api for that.

– Jim B.
Nov 11 '18 at 22:34













@JimB. is correct, you would need to create an API endpoint for posting the data back to the server, and then doing something with it. The other way you could do it would be with something like websockets between the client and the server.

– Joshua Terrill
Nov 12 '18 at 0:31





@JimB. is correct, you would need to create an API endpoint for posting the data back to the server, and then doing something with it. The other way you could do it would be with something like websockets between the client and the server.

– Joshua Terrill
Nov 12 '18 at 0:31












1 Answer
1






active

oldest

votes


















0















How do I access variable x in app.js, since it is not declared there.




You don't. Not directly. Variable x is inside the browser running on the client computer and is not directly available or visible to your server or your server computer.



When you have a variable in the Javascript inside the web page that is running in the browser, the only way to get that to your server is to make an Ajax call to your server or to embed that value in an URL and request a new page with that value in the URL (as part of the path or a query parameter).



To understand further, let's review a little about how an EJS file works from your node.js server.



  1. Browser requests page (that is represented by your EJS file).

  2. Your server receives request for that page.

  3. Server collects any relevant data for that page, puts that data into an object, then renders the EJS page. That will expand the EJS page and incorporate any desired data into the page.

  4. The rendered EJS page is sent to the browser as the response to the original HTTP request.

  5. Browser receives the now HTML page (rendered EJS page), parses it and displays it.

  6. Browser parses and runs any script tags in the page.

At the point where scripts are running in the browser, they are a completely separate environment from your server. They are running on the client computer in the browser, not the server computer. To share data from the web page with the server, you have to initiate some communication with the server and send the server some data. There are a number of ways to do that. For example, you could make an Ajax call to the server, send it the data from the web page, have the server received that Ajax request and then send back a response. Your client-side Javascript will then get back that Ajax response and can then do whatever it wants with that result (insert it into the web page, load a different page, show it to the user, etc...).




If you told us more specifically what you're trying to do with value x, then we could make a more detailed recommendation on how to achieve that.






share|improve this answer























  • I want to pass the value of variable in a sql query which is written in app.js

    – Apoorv
    Nov 12 '18 at 7:51











  • @Apoorv - Create a route on your server that you can send an Ajax call to from your web page with the value of x as a query parameter and your server will then parse the query parameter, use it in the sql query and return the results of the sql query back as the response to the client's Ajax call. That's what Ajax is used for.

    – jfriend00
    Nov 12 '18 at 8:12












  • Thank you for the response. I'll try to do it.

    – Apoorv
    Nov 12 '18 at 19:03











  • I researched a lot but did any tutorial or resource to achive this. Please tell me how to do this.

    – Apoorv
    Nov 15 '18 at 19:38










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253289%2fhow-to-include-an-ejs-file-in-app-js-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















How do I access variable x in app.js, since it is not declared there.




You don't. Not directly. Variable x is inside the browser running on the client computer and is not directly available or visible to your server or your server computer.



When you have a variable in the Javascript inside the web page that is running in the browser, the only way to get that to your server is to make an Ajax call to your server or to embed that value in an URL and request a new page with that value in the URL (as part of the path or a query parameter).



To understand further, let's review a little about how an EJS file works from your node.js server.



  1. Browser requests page (that is represented by your EJS file).

  2. Your server receives request for that page.

  3. Server collects any relevant data for that page, puts that data into an object, then renders the EJS page. That will expand the EJS page and incorporate any desired data into the page.

  4. The rendered EJS page is sent to the browser as the response to the original HTTP request.

  5. Browser receives the now HTML page (rendered EJS page), parses it and displays it.

  6. Browser parses and runs any script tags in the page.

At the point where scripts are running in the browser, they are a completely separate environment from your server. They are running on the client computer in the browser, not the server computer. To share data from the web page with the server, you have to initiate some communication with the server and send the server some data. There are a number of ways to do that. For example, you could make an Ajax call to the server, send it the data from the web page, have the server received that Ajax request and then send back a response. Your client-side Javascript will then get back that Ajax response and can then do whatever it wants with that result (insert it into the web page, load a different page, show it to the user, etc...).




If you told us more specifically what you're trying to do with value x, then we could make a more detailed recommendation on how to achieve that.






share|improve this answer























  • I want to pass the value of variable in a sql query which is written in app.js

    – Apoorv
    Nov 12 '18 at 7:51











  • @Apoorv - Create a route on your server that you can send an Ajax call to from your web page with the value of x as a query parameter and your server will then parse the query parameter, use it in the sql query and return the results of the sql query back as the response to the client's Ajax call. That's what Ajax is used for.

    – jfriend00
    Nov 12 '18 at 8:12












  • Thank you for the response. I'll try to do it.

    – Apoorv
    Nov 12 '18 at 19:03











  • I researched a lot but did any tutorial or resource to achive this. Please tell me how to do this.

    – Apoorv
    Nov 15 '18 at 19:38















0















How do I access variable x in app.js, since it is not declared there.




You don't. Not directly. Variable x is inside the browser running on the client computer and is not directly available or visible to your server or your server computer.



When you have a variable in the Javascript inside the web page that is running in the browser, the only way to get that to your server is to make an Ajax call to your server or to embed that value in an URL and request a new page with that value in the URL (as part of the path or a query parameter).



To understand further, let's review a little about how an EJS file works from your node.js server.



  1. Browser requests page (that is represented by your EJS file).

  2. Your server receives request for that page.

  3. Server collects any relevant data for that page, puts that data into an object, then renders the EJS page. That will expand the EJS page and incorporate any desired data into the page.

  4. The rendered EJS page is sent to the browser as the response to the original HTTP request.

  5. Browser receives the now HTML page (rendered EJS page), parses it and displays it.

  6. Browser parses and runs any script tags in the page.

At the point where scripts are running in the browser, they are a completely separate environment from your server. They are running on the client computer in the browser, not the server computer. To share data from the web page with the server, you have to initiate some communication with the server and send the server some data. There are a number of ways to do that. For example, you could make an Ajax call to the server, send it the data from the web page, have the server received that Ajax request and then send back a response. Your client-side Javascript will then get back that Ajax response and can then do whatever it wants with that result (insert it into the web page, load a different page, show it to the user, etc...).




If you told us more specifically what you're trying to do with value x, then we could make a more detailed recommendation on how to achieve that.






share|improve this answer























  • I want to pass the value of variable in a sql query which is written in app.js

    – Apoorv
    Nov 12 '18 at 7:51











  • @Apoorv - Create a route on your server that you can send an Ajax call to from your web page with the value of x as a query parameter and your server will then parse the query parameter, use it in the sql query and return the results of the sql query back as the response to the client's Ajax call. That's what Ajax is used for.

    – jfriend00
    Nov 12 '18 at 8:12












  • Thank you for the response. I'll try to do it.

    – Apoorv
    Nov 12 '18 at 19:03











  • I researched a lot but did any tutorial or resource to achive this. Please tell me how to do this.

    – Apoorv
    Nov 15 '18 at 19:38













0












0








0








How do I access variable x in app.js, since it is not declared there.




You don't. Not directly. Variable x is inside the browser running on the client computer and is not directly available or visible to your server or your server computer.



When you have a variable in the Javascript inside the web page that is running in the browser, the only way to get that to your server is to make an Ajax call to your server or to embed that value in an URL and request a new page with that value in the URL (as part of the path or a query parameter).



To understand further, let's review a little about how an EJS file works from your node.js server.



  1. Browser requests page (that is represented by your EJS file).

  2. Your server receives request for that page.

  3. Server collects any relevant data for that page, puts that data into an object, then renders the EJS page. That will expand the EJS page and incorporate any desired data into the page.

  4. The rendered EJS page is sent to the browser as the response to the original HTTP request.

  5. Browser receives the now HTML page (rendered EJS page), parses it and displays it.

  6. Browser parses and runs any script tags in the page.

At the point where scripts are running in the browser, they are a completely separate environment from your server. They are running on the client computer in the browser, not the server computer. To share data from the web page with the server, you have to initiate some communication with the server and send the server some data. There are a number of ways to do that. For example, you could make an Ajax call to the server, send it the data from the web page, have the server received that Ajax request and then send back a response. Your client-side Javascript will then get back that Ajax response and can then do whatever it wants with that result (insert it into the web page, load a different page, show it to the user, etc...).




If you told us more specifically what you're trying to do with value x, then we could make a more detailed recommendation on how to achieve that.






share|improve this answer














How do I access variable x in app.js, since it is not declared there.




You don't. Not directly. Variable x is inside the browser running on the client computer and is not directly available or visible to your server or your server computer.



When you have a variable in the Javascript inside the web page that is running in the browser, the only way to get that to your server is to make an Ajax call to your server or to embed that value in an URL and request a new page with that value in the URL (as part of the path or a query parameter).



To understand further, let's review a little about how an EJS file works from your node.js server.



  1. Browser requests page (that is represented by your EJS file).

  2. Your server receives request for that page.

  3. Server collects any relevant data for that page, puts that data into an object, then renders the EJS page. That will expand the EJS page and incorporate any desired data into the page.

  4. The rendered EJS page is sent to the browser as the response to the original HTTP request.

  5. Browser receives the now HTML page (rendered EJS page), parses it and displays it.

  6. Browser parses and runs any script tags in the page.

At the point where scripts are running in the browser, they are a completely separate environment from your server. They are running on the client computer in the browser, not the server computer. To share data from the web page with the server, you have to initiate some communication with the server and send the server some data. There are a number of ways to do that. For example, you could make an Ajax call to the server, send it the data from the web page, have the server received that Ajax request and then send back a response. Your client-side Javascript will then get back that Ajax response and can then do whatever it wants with that result (insert it into the web page, load a different page, show it to the user, etc...).




If you told us more specifically what you're trying to do with value x, then we could make a more detailed recommendation on how to achieve that.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 0:22









jfriend00jfriend00

433k55562611




433k55562611












  • I want to pass the value of variable in a sql query which is written in app.js

    – Apoorv
    Nov 12 '18 at 7:51











  • @Apoorv - Create a route on your server that you can send an Ajax call to from your web page with the value of x as a query parameter and your server will then parse the query parameter, use it in the sql query and return the results of the sql query back as the response to the client's Ajax call. That's what Ajax is used for.

    – jfriend00
    Nov 12 '18 at 8:12












  • Thank you for the response. I'll try to do it.

    – Apoorv
    Nov 12 '18 at 19:03











  • I researched a lot but did any tutorial or resource to achive this. Please tell me how to do this.

    – Apoorv
    Nov 15 '18 at 19:38

















  • I want to pass the value of variable in a sql query which is written in app.js

    – Apoorv
    Nov 12 '18 at 7:51











  • @Apoorv - Create a route on your server that you can send an Ajax call to from your web page with the value of x as a query parameter and your server will then parse the query parameter, use it in the sql query and return the results of the sql query back as the response to the client's Ajax call. That's what Ajax is used for.

    – jfriend00
    Nov 12 '18 at 8:12












  • Thank you for the response. I'll try to do it.

    – Apoorv
    Nov 12 '18 at 19:03











  • I researched a lot but did any tutorial or resource to achive this. Please tell me how to do this.

    – Apoorv
    Nov 15 '18 at 19:38
















I want to pass the value of variable in a sql query which is written in app.js

– Apoorv
Nov 12 '18 at 7:51





I want to pass the value of variable in a sql query which is written in app.js

– Apoorv
Nov 12 '18 at 7:51













@Apoorv - Create a route on your server that you can send an Ajax call to from your web page with the value of x as a query parameter and your server will then parse the query parameter, use it in the sql query and return the results of the sql query back as the response to the client's Ajax call. That's what Ajax is used for.

– jfriend00
Nov 12 '18 at 8:12






@Apoorv - Create a route on your server that you can send an Ajax call to from your web page with the value of x as a query parameter and your server will then parse the query parameter, use it in the sql query and return the results of the sql query back as the response to the client's Ajax call. That's what Ajax is used for.

– jfriend00
Nov 12 '18 at 8:12














Thank you for the response. I'll try to do it.

– Apoorv
Nov 12 '18 at 19:03





Thank you for the response. I'll try to do it.

– Apoorv
Nov 12 '18 at 19:03













I researched a lot but did any tutorial or resource to achive this. Please tell me how to do this.

– Apoorv
Nov 15 '18 at 19:38





I researched a lot but did any tutorial or resource to achive this. Please tell me how to do this.

– Apoorv
Nov 15 '18 at 19:38



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253289%2fhow-to-include-an-ejs-file-in-app-js-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)