I have a function that loads more database results. This function is called on different pages so i have given its own file and then linked to it in each of those pages e.g.:
<script type="text/javascript" src="js/loadmorebuilds.js">
</script>
In that file, i run an ajax call to a file and use a segment of the pages URL.
If i create that segment in the file itself it doesnt work as im guessing it uses the URL of the page. My work around currently is like so:
<script>
var exampleSegment = window.location;
</script>
<script type="text/javascript" src="js/loadmorebuilds.js">
</script>
However i am writing a longer method of getting that segment from the URL and want to include it in with the js file itself so i dont have to go editing it on every page that i need it.
Is there a way to do this? Thanks, hope it makes sense...
So what you're asking is how you can get the url of the external script to go from there instead?
If the script is loaded with the page, it will be the last on the page as of the script running. So you can use a code like:
var scripts = document.getElementsByTagName('script');
var lastScript = scripts[scripts.length - 1];
alert(lastScript.src);
Source is another post on this forum Can code in a Javascript file know its own domain/URL
Related
I have a script.js file linked to all of the pages on my site.
On only one page in particular, the "About" page, I don't want a certain function from script.js, called myFunction, to run, because it messes with the formatting on that page.
However, I do still need to use the rest of the JS from script.js on this page, so I don't want to unlink the script.js file and copy every single line of code from script.js except myFunction onto the About page locally, as then I will need to update that page manually every time I add new JS to the script.js file.
Is there a way to tell a specific page to ignore a certain function from script.js? I thought this would be easy to find an answer to but all I could find were results for doing this in WordPress with php.
I would like to use vanilla JS if possible.
Add this code where you call your function on script.js
var pathname = window.location.pathname;
if(pathname != 'about.html'){
myFunction();
}
I'm trying to insert that Mcafee Security etiquette into my web page. Mcafee gave me the following code:
<script type="text/javascript" src="https://cdn.ywxi.net/js/1.js" async> </script>
However, it does not appear on my page when I add it.
I'm adding in the <head> tag. The page to which I am inserting is in the following location:
/home/apache2/old/newmail/skins/george
Login.html
You need to make sure the website you are trying to insert it on is registered and currently being used. The script looks at the location.host, here would be stackoverflow.combefore sending the code to display the actual element. Note the line with setting the src attribute.
try{
var v=document.createElement("script");
v.setAttribute("type","text/javascript");
v.setAttribute("src","//cdn.ywxi.net/js/host-loader.js?h="+location.host);
document.getElementsByTagName("head")[0].appendChild(v)
}catch(e){};
You can test this by going to the website cdn.ywxi.net/js/host-loader.js?h=<YOUR HOST HERE> and confirm there is JavaScript sent back.
You need to put the code into
/usr/share/apache2/roundcubemail-1.0.1/skins/larry/includes/links.html
I have a script include on a page as following.
<script src="https://domain/demo.js" type="text/html" id="google_script"></script>
Once the demo.js is loaded, I need to grab the content of loaded demo.js without recalling it with ajax. Please share your thoughts if that's possible and how.
Sadly no.
With pure JavaScript you can't get content of other JavaScript file without executing it.
Possible solutions:
Use PHP
Save your JS file as text file and then load it to iframe and get the source
Get the source, but run the script
EDIT:
With JavaScript you can't get full script, but function like:
var funcsourcecode = functionname.toString();
I am given this code which should display an embedded small coupon version of this page https://weedmaps.com/deals#/1118217:
<script type="text/javascript">var coupon_id = 17811;</script>
<script type="text/javascript">var coupon_type = "deliveries";</script>
<script type="text/javascript" src="https://weedmaps.com/embed/coupon.js"></script>
I do not know how to add the JavaScript to the HTML correctly. I have placed the following scripts in the head section. But I don't understand how to have the coupon generate in the div I want it to. I have tried calling the JavaScript function, but I've never worked with JavaScript before. Could someone please help me embed this coupon.
I've taken a look at your script and first of all: it definitely should be placed inside the document and not into the <head> section because coupon.js is writing out html at the placement of the coupon.js script import.
In theory you just need to place the script and it should work but there are some problems:
You need to execute it on a web server - when running as a plain html file the script just tries to find the libraries in your file system which does not work.
It still would not work because it would still try to find some resources at your web-server. In mycase it the script tried to load http://localhost:63342/restpoints/deliveries/17811/deal which will not work
To prove 2. just try https://weedmaps.com/restpoints/deliveries/17811/deal with the correct domain. Then you are receiving correct JSON which is used to fill the coupon pane.
=> Consequently the script you were given has problems if it should be executable from domains different from "weedmaps.com"
Javascript can be between head tag but it advisable to put it below before the body closing tag, to allow your page contents loads first before loading javascript. Just import your javascript. and call out. Hope this was helpful.
<!DOCTYPE html>
<html>
<head>
</head>
var coupon_id = 17811;
The JS indicates it is looking for an element with an id of #weedCouponPane. Do you have this in your html? i.e.
<div id="weedCouponPane"></div>
With a bit of help from Stackoverflow I've managed to display blog content from Tumblr on my own website.
I've noticed that if the Tumblr site is slow, it effects the page loading speed of my own website.
I found this article on javascript.info. The article recommends using this code to create a variable of the external script URL.
var script = document.createElement('script')
script.src = 'http://ads.com/buyme?rand='+Math.random()
// now append the script into HEAD, it will fetched and executed
document.documentElement.firstChild.appendChild(script)
I (kind of) understand what's happening in the first two lines, but I don't understand how to use the last bit of the code:
document.documentElement.firstChild.appendChild(script)
How would I add the script variable into the src of the script?
<script type="text/javascript" src="URL OF SCRIPT TO GO HERE"></script>
Hope someone can help, this'd be useful for other third party scripts on my site.
UPDATE
Thanks to 'The Spooniest' for helping with this (see thread below).
I was having problems getting this to work with my current page, so I decided to strip it down and create a basic page to see if I could get just this script working.
The code below works, it drops the Javascript just before the </head> tag. Great! However there's somehting that bothers me when I inspect the page (using Dev Tools) I see this warning:
Uncaught SyntaxError: Unexpected token < ?rand=0.9741437959019095:1
Any ideas why this appears? Is it a problem?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>
var script = document.createElement('script')
script.src = 'http://ads.com?rand=' + Math.random()
// now append the script into HEAD, it will fetched and executed
document.documentElement.firstChild.appendChild(script)
</script>
<title>Demo</title>
</head>
<body>
<p>A basic page</p>
</body>
</html>
UDATE 2
Figured out what the problem was. The URL in the script wasn't a .js file. When I enter a URL linking to a .js file (which I want) it works a treat!
You already added it.
Let's look over the code again:
var script = document.createElement('script')
script.src = 'http://ads.com/buyme?rand='+Math.random()
// now append the script into HEAD, it will fetched and executed
document.documentElement.firstChild.appendChild(script)
The first line creates the element that will become your script tag. The second line sets script.src to a particular URL: in this case it's to an ad site, but you would want to use the URL of your Tumblr code instead. The third line puts the your script at the end of the first tag inside the document element (which in this case means the head).
The magic happens in the first two lines. document.createElement looks at the name of the tag you're creating, in order to figure out the proper class to use for the element: for 'script', that means using HTMLScriptElement. This particular class knows that when you set its src property, that's supposed to go in the src attribute of the script tag. The instance you created remembers this even after you put it into the document, and so that's how the attribute gets there.
document.documentElement.firstChild.appendChild(script)
The above code will add the script to the head tag of your page. As document.documentElement.firstChild will point to head tag.
You don't have to add script variable into src, since it's already taken care by javascript code.
It's generally suggested that javasript files should always be added at the bottom of the page. So that the page loads faster.