GET Tags on Javascript? - javascript

I'm working on a script that requires I get a variable so we goto the right location on the site.
I want to do something like:
<script type="text/javascript" src="dir/script.js?x=103"></script>
But how would I get that?

Assuming you know the filename of the script within the script itself, search all <script> tags for the one that contains the relevant filename in its src attribute. Then split it on ? and read the value.

Related

Disable script tag in html file or change script tag type/src using javascript

I want to disable the script tag or if its possible to change script src or type in this script using javascript?
<script type="text/javascript" src="./1.js.download"></script>
I can't add id in this code.
please help and feel free to ask anything if you don't understand
How about removing it altogether? The example is a simple function that get an element by the given selector and remove it. FYI if you don't want the <script> to actually load, then your'e out of luck. It'll be parsed as long as it's in the HTML, there's no way to avoid it from loading by JavaScript.
In the example there are 3 <script> tags and the target is in the middle. document.querySelector("script") will stop at the first match so you'll need a more specific selector. To target the second tag I used script:nth-of-type(2). You can verify success using by F12
const killTag = selector => document.querySelector(selector).remove();
killTag(`script:nth-of-type(2)`);
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="./1.js.download"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js"></script>
You can remove src from script tag like this:
s = document.querySelectorAll('script')
console.log(s[1])
s[1].setAttribute('src', '')
console.log(s[1])
<script type="text/javascript" src="./1.js.download"></script>
You will have maybe multiple scripts in your dom. then you have take a look which element it is and change the key from the collection. in this case the key was 1.
If you need to select specific script element you can get it by attribute like this:
let ok = document.querySelector('[src="./1.js.download"]');
//Then you can either change id or type:
ok.setAttribute('id', 'ok');
ok.setAttribute('type', 'module');

Access value on window from Google Tag Manager custom html tag

I have some javascript I am using a GTM custom html tag to inject on my page. I need to access a value that is on the window, is there a way to do this from GTM or do I need to put the script in my actual html now to get this value for my script?
Here is an example of what I have now that is not working:
<script type="text/javascript">
console.log(window.awesomeValue);
</script>
When I inspect the source after GTM injects the script the window.awesomeValue appears literally and is not being evaluated.
Thanks!
You will need to use a JavaScript variable to hold the value. Try this:
Create a JavaScript Variable and set the value to window.awesomeValue
Modify your custom HTML tag to use the newly created variable:
Your final code will look like:
<script type="text/javascript">
console.log({{yourNewJSVariable}});
</script>

Force browser to reload javascript files

I tried like this
<script type="text/javascript" src="myfile.js"?+Date.now()></script>
In browser it is showing as it is.
I want to add some timestamp to each js files.
i.e.,
<script type="text/javascript" src="myfile.js"?+5671836294></script>
Thanks in advance.
You can't randomly stick JavaScript anywhere you like in HTML.
When you are in the middle of an HTML start tag you can either:
End the tag with >
Write an attribute
JavaScript does not belong there.
If you want to generate an HTML attribute value dynamically when the element is created, then you must create the entire element with JavaScript.
e.g.
<script>
var s = document.createElement("script");
s.src = "myfile.js"?+Date.now();
document.head.appendChild(s);
</script>
… but you'd probably be better of solving this problem by properly configuring your HTTP headers for the script instead.
Refresh the page or rewrite the script tag and append it to the bottom of the body.
But once a page is loaded, it is loaded. You have to redeclare to overwrite.

Can't append script element to head

Current variant looks like that (I tried solution offered here: Can't append <script> element):
var s=document.createElement("script");
s.type="text/javascript";
s.src="js/properties.js";
$("head").append(s);
Previous variant was:
$("head").append($('<script type="text/javascript" src="js/properties.js"></script>'));
And both of them don't work. "properties.js" is also in "js" folder, but if I remove this part of path, it doesn't change anything.
I also tried to use ' instead " and check addBlock: I had it installed, but it's disabled on this page.
Changing "append" function to "appendChild" also didn't help.
"properties.js" contains just one line:
var PREFIX_URL = "http://localhost:8080/app-rest-1.0.0-SNAPSHOT";
And firstly I declare it in "main.js" to which I, in fact, try to connect this file.
Explain, please, what I'm doing wrong.
Add all your <script> tags right before the closing </body> tag, because when the browser encounters a <script> tag it begins downloading it and stops rendering of the page. So by placing them at the bottom of the page you make sure your page is fully loaded before trying to interact with the DOM elements. Also $("head") returns an array of all the <head> tags. You should also enclose your calls in a $(document).ready() function.
<!-- Your html tags here -->
<script type="text/javascript">
$(document).ready(function(){
var s=document.createElement("script");
s.type="text/javascript";
s.src="js/properties.js";
$("head")[0].append(s);
});
</script>
</body>
I made JSBin example. You can see in console that last script tag is the one you need. So the your code is correct.
If your IDE don't highlight 'var' - it may be error not in javascript. You can place it in a wrong place for example.
Can you provide link to a gist (or pastie.org or smth) for us to better understand your problem.
P.S. The code $("head")[0].append gives me undefined ( note to previous answer)

Dynamic javascript reference

I've inherited some code (not mine- I swear!) which uses a session variable in the header of the HTML to determine which javascript file to link to.
i.e.
<SCRIPT language="javascript" src="../JavaScript/<%=Session("jsFileName")%>.js"></SCRIPT>
It does work, except that it won't let me change to design view. It gives the message
"Could not open in design view. Quote Values differently inside a '<%... "value" ...%>' block."
Anyone got any suggestions as to a workaround, that doesn't involve a huge rewrite.
Try this:
<SCRIPT language="javascript" src='../JavaScript/<%=Session("jsFileName")%>.js'></SCRIPT>
Notice use of ' instead of " for src attribute.
Can't you just comment out the SCRIPT tag in code view and then swith to design view?
Or replace the outer double quotes with single so you have:
src='../JavaScript/<%=Session("jsFileName")%>.js'

Categories