Pass vars to JavaScript via the SRC attribute
<script>
var config=true;
</script>
<script src="myscript.js"></script>
You can't pass variables to JS the way you tried. SCRIPT tag does not create a Window object (which has a query string), and it is not server side code.
Pass variable to this.src
With jQuery by using mouseover()
and mouseout()
and attr()
methods.
$(document).ready(function(){
var file_name='your_file_name';
var new_source_for_image ="/prestashop_1.7.0.3/modules/se_daynight/img/" + file_name + ".png";
var new_source_for_image_with_glow = "/prestashop_1.7.0.3/modules/se_daynight/img/" + file_name + "_glow" + ".png";
$("img#visit_image").attr('src',new_source_for_image);
$("img#visit_image").mouseover(function(){
$(this).attr('src',new_source_for_image_with_glow);
});
$("img#visit_image").mouseout(function(){
$(this).attr('src',new_source_for_image);
});
});
In Javascript, is it possible to pass a variable into <script> src parameter?
I can see two solutions here.
First: you can process those GET parameters on the server where the twitter.js is hosted, so that it will dynamically change the js file. For example, you file is:
var handle = {{ handle }};
And your server somehow processes the file, replacing that twitter.js template file dependent on what request was sent.
The second option would be to set the global variables on the page where twitter.js is loaded, like this:
<script type="text/javascript">
window.twitter_js_handle = 'aplusk';
</script>
<script type="text/javascript" src="http://domain.com/twitter.js" />
And in twitter.js:
var handle = window.twitter_js_handle || null;
Passing javascript arguments via script src attribute
Cant you do something like this?
<script>
var state = 1;
</script>
<script src="http://domain.com/js/script.js">
How to pass javascript variable to javascript src link
Give it an id
, which then allows you to target it like any other element, then change its src
:
(inspect the snippet's result to see the change)
const theScript = document.getElementById("changeMySrc"), theVariable = "something"
theScript.src = "http://somesite.com/" + theVariable + "/urlEnd"
<script id="changeMySrc"></script>
How can I use a variable in the src of a script tag?
How do you pass a variable as a part of an URL?
Easy! Since you don't mind using javascript a simple solution is as follows
var scriptElement = document.createElement("script");
scriptElement.src = "http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=" + encodeURIComponent("$SPX+" + sym) + "&Output=JS";
document.head.appendChild(scriptElement);
This code creates an script element with the source being "http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=" + encodeURIComponent("$SPX+" + sym) + "&Output=JS". Then it appends it to the head of the webpage.
The final HTML being:
<script>
var sym = "MSFT";
document.write("\<script src='" + "http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=" + encodeURIComponent("$SPX+" + sym) + "&Output=JS" + "'\>\</script\>");
</script>
The reason that this solution won't work is as follows:
Codepen is an HTTPS site which means it WILL NOT serve HTTP content. To be honest I have no idea why Codepen serves the HTTP script. How to fix this? Well, there really is only one easy solution:
Instead of using Codepen use a local HTML file and just open that, if you have your own HTTP server you can use that.
Quick how-to guide:
1) Open a text editor
2) Type the following
<html>
<head>
<script>
var sym = "MSFT";
document.write("\<script src='" + "http://markets.financialcontent.com/stocks?Module=snapshot&Ticker=" + encodeURIComponent("$SPX+" + sym) + "&Output=JS" + "'\>\</script\>");
</script>
</head>
<body>
</body>
</html>
3) Now hit the "Save As" button and save it as a .html file
4) Open it!
5) Have fun :)
Related Topics
Document.Write() Overwriting the Document
JavaScript - Shuffle HTML List Element Order
Positioning ≪Div≫ Element At Center of Screen
Can Jquery Get All CSS Styles Associated With an Element
How to Disable a Browser or Element Scrollbar, But Still Allow Scrolling With Wheel or Arrow Keys
Webkit-Based Blurry/Distorted Text Post-Animation Via Translate3D
How to Correctly Detect Orientation Change Using Phonegap on Ios
Var Functionname = Function() {} VS Function Functionname() {}
Find Object by Id in an Array of JavaScript Objects
Changing the Selected Option of an HTML Select Element
Html ≪Select≫ Selected Option Background-Color CSS Style
How to Use External ".Js" Files
Html5 Local Storage Fallback Solutions
How to Remove a Style Added With .Css() Function
Check Element CSS Display With JavaScript
How to Invoke Objective-C Method from JavaScript and Send Back Data to JavaScript in Ios