What is my script src URL?
Put this in the js file that needs to know it's own url.
Fully Qualified (eg http://www.example.com/js/main.js
):
var scriptSource = (function(scripts) {
var scripts = document.getElementsByTagName('script'),
script = scripts[scripts.length - 1];
if (script.getAttribute.length !== undefined) {
return script.src
}
return script.getAttribute('src', -1)
}());
Or
As it appears in source (eg /js/main.js
):
var scriptSource = (function() {
var scripts = document.getElementsByTagName('script'),
script = scripts[scripts.length - 1];
if (script.getAttribute.length !== undefined) {
return script.getAttribute('src')
}
return script.getAttribute('src', 2)
}());
See http://www.glennjones.net/Post/809/getAttributehrefbug.htm for explanation of the getAttribute
parameter being used (it's an IE bug).
What is the right way to write my script 'src' url for a local development environment?
Write the src tag for calling the js file as
<script type='text/javascript' src='../Users/myUserName/Desktop/myPage.js'></script>
This should work.
JavaScript - How do I get the URL of script being called?
From http://feather.elektrum.org/book/src.html:
var scripts = document.getElementsByTagName('script');
var index = scripts.length - 1;
var myScript = scripts[index];
The variable myScript
now has the script dom element. You can get the src url by using myScript.src
.
Note that this needs to execute as part of the initial evaluation of the script. If you want to not pollute the Javascript namespace you can do something like:
var getScriptURL = (function() {
var scripts = document.getElementsByTagName('script');
var index = scripts.length - 1;
var myScript = scripts[index];
return function() { return myScript.src; };
})();
Change script src by URL param and make it functional
I think you should remove the script tag and re-add the updated one.
let oldScriptTag = document.getElementById('results');
let newScriptTag = document.createElement('script');
newScriptTag.id = 'results';
newScriptTag.src = 'path/to/script/' + yrs + '/' + wks + '.js';
newScriptTag.textContent = '//script';
var body = document.getElementsByTagName('body')[0];
oldScriptTag.remove();
body.appendChild(newScriptTag);
What is a ? for in the src attribute of a html script tag?
I believe what the author was doing was ensuring that if he creates version 3.3 of his script he can change the version= in the url of the script to ensure that users download the new file instead of running off of the old script cached in their browser.
So in this case it is part of the caching strategy.
JavaScript - how to retrieve the src= url from the script contents?
This page describes a method for getting these values:
<script type="text/javascript"
src="scriptaculous.js?load=effects,builder"></script>
And the javascript:
function getJSvars(script_name, var_name, if_empty) {
var script_elements = document.getElementsByTagName(‘script’);
if(if_empty == null) {
var if_empty = ”;
}
for (a = 0; a < script_elements.length; a++) {
var source_string = script_elements[a].src;
if(source_string.indexOf(script_name)>=0) {
var_name = var_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex_string = new RegExp("[\\?&]"+var_name+"=([^]*)");
var parsed_vars = regex_string.exec(source_string);
if(parsed_vars == null) { return if_empty; }
else { return parsed_vars[1]; }
}
}
}
Figuring out the base URL where JS script was loaded from within a script
iframe.html and script.js are unique across all enviroments
Assuming that the script.js
file name is pre-determined and unique, we can search for a script tag that imports the given script. Then fetch the full source url from the script tag.
Note: In the example below I am using jquery simply because its available in the SO sandbox.
const fileName = 'jquery.min.js';
// src$="foo" is equivalent to "where the value of the src field ends with 'foo' "const $script = document.querySelector(`script[src$="${fileName}"]`);const scriptSrc = $script.src;
console.log(scriptSrc);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Is there a way to change script src=?? url value in chrome and reload page?
I used to do this this way :
- Go to your web page
- View source-code
- Copy and paste it to a new file.html or whatever on local
If this shows you the "correct" page design, then now you can copy/paste your js file the same way, change the import of js in your file.html and make your trys
How can I get the content of the file specified as the 'src' of a script tag?
Do you want to get the contents of the file http://www.example.com/script.js? If so, you could turn to AJAX methods to fetch its content, assuming it resides on the same server as the page itself.
Related Topics
How to Add Many Functions in One Ng-Click
Where to Save a Jwt in a Browser-Based Application and How to Use It
Injecting a Mock into an Angularjs Service
Get Loop Counter/Index Using For…Of Syntax in JavaScript
How to Get Subarray from Array
Getting "Typeerror: Failed to Fetch" When the Request Hasn't Actually Failed
How to Do a Deep Comparison Between 2 Objects with Lodash
How to Check If the Url Contains a Given String
Differencebetween Parseint(String) and Number(String) in JavaScript
How to Remove Element from an Array in JavaScript
JavaScript Function Order: Why Does It Matter
What Is {This.Props.Children} and When You Should Use It
How to Put an Image File in a JSON Object
React - Preventing Form Submission
How to Add/Subtract Dates with JavaScript
Referenceerror: Fetch Is Not Defined
Componentdidmount Equivalent on a React Function/Hooks Component