how to pass variable to external js file

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL), i want to send some url path to external js file. By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. In controller you can store in ViewData or ViewBag, in cshml ( i havent tried but should work). Teams. Modified 8 years, 11 months . Set it in localStorage with: and refer to it inside javascript file like: It's possible to pass parameters to js modules and read them after via import.meta.url. Solution 2 There are no "external" or "internal" JavaScript files. To make it global (not ideal): window.x = "sachin"; To add to a common namespace (keep in mind this namespace is global too): Create a namespace in a third file, MYAPP = {}; Use namespace in second file: MYAPP.x = "sachin;" Can you access or var_dump the variables "phone" and "laptop" on the view? If its writing the portion of code to parse the string that bothers you, perhaps another way would be to json encode your variables and put them in something like the rel attribute of the tag? old but for those googling: this is great if you need to work around a CSP (content security policy). Require dependencies: Express JS EJS npm i express ejs Step 3: Create a new server.js file in the Project directory. @Sarfraz, is there any other way to ensure the variable will be global? How to pass variable from one Javascript to another Javascript file [duplicate], Global variables in Javascript across multiple files, Why on earth are people paying for digital real estate? Now to access that data in the script tag of EJS file or the .js file all you need to do is to pass that data in another variable as below: let data = '<%-data%>' Now you can perform any operation on the data variable which has the same value as EJS passed data variable. Find centralized, trusted content and collaborate around the technologies you use most. i added an example in my question. Remove outermost curly brackets for table of variable dimension. Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide, The future of collective knowledge sharing, use with global variable and order the js file one by one, make sure the file with the variable is before the other file. in this case using Jquery: Although this question has been asked a while ago, it is still relevant as of today. Code between <%- and %> is server side code. EDIT: How can I validate an email address in JavaScript? Problem with passing a parameter in JavaScript. So I have to store the data for each user and the have to load the data. I'm using Laravel and I want to pass variable to external js file A bunch of my JavaScript code is in an external file called helpers.js. I have attempted to create a global variable by defining: var myFunctionTag = true; In global scope both in my HTML code and in helpers.js. And the data (selected regions) can be different do different users. And access it from fileB.js like: I may be misunderstanding what the op asked because I'm not good at javascript, but this allowed me to pass variables from my view page so my external js file had access to the variables. Why free-market capitalism has became more associated to the right than to the left, to which it originally belonged? rev2023.7.7.43526. By clicking Post Your Answer, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct. By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. This works in all recent browsers. What would stop a large spaceship from looking like a flying brick? Pass/Fail Grade Actions: Set what actions the quiz will carry out if the participant passes the quiz, or fails. spelling and grammar. Can't get ejs value into script variable? If you move it out of the EJS template file that generates HTML and into a different EJS template file that generates JavaScript then it will work as normal (except that it will have a different URL with a different end point in your server side code, so you will need to move the server side code which populates the variables you are trying to access). But you can pass it to view, where you use the src of chart.js, then you can use the variable with chart.js. Two of the simplest ways would be to make the variable global or add it to a particular namespace that both files share. ChatGPT) is banned, Can I pass a parameter directly to a .js file, and how do I get the value, Pass Argument to Javascript from HTML file. I liked this solution, but modified it slightly, to use the fragment identifier, so that it doesn't bust caches: var frag = myScript.src.replace(/^[^\#]+\#?/,''); Oh, and I found that putting JSON in there works, as long as you urlencode it. To learn more, see our tips on writing great answers. rev2023.7.7.43526. Possible Duplicate: How do I solve this? To learn more, see our tips on writing great answers. Find centralized, trusted content and collaborate around the technologies you use most. It's not valid html (I don't think) but it seems to work if you create a custom attribute for the script tag in your webpage: