External Javascript File - QueryString Info Available? - javascript

On an HTML page I have a reference to an external Javascript file like this:
<script src="http://MyServer.com/js/myscript.js?Happy=True"></script>
Inside the myscript.js when it runs, can I get the Happy=True QueryString-like part of the js source URL?
Note I do not want the URL of the HTML page, I need to get the URL of the js file.
My Guess is no.

You will be able to detect the src-attribute of the script-element(would be easier to locate if the <script> has an ID). Out of that URL you could extract the Query-String.

Related

how can i access a web page domain in java script without adding java script file to the html file scripts?

I'm a medium-level programmer in javascript and I was anxious about how to have access to the domain of a web page without adding the file into the page script. I appreciate any help.
If you mean how to get the current page domain you can use window.location.hostname
If you don't want to "add a JavaScript file to the HTML file", the only alternative is to embed the JavaScript code into the HTML code (which I don't recommend) by using <script> tags.
<script>
console.log(window.location.hostname);
</script>

How to refer javascript file in html present in another folder

I have a JS file in folder
/admintool/src/main/webapp/static/js/itemInventory.js
i have html file in folder
/admintool/src/main/webapp/WEB-INF/templates/itemInventory.ms
On page load my starts with url - http://localhost:8080/admintool/selectItem/showAllItems
Inside itemInventory.ms file, when i use the below code to refer that javascript, it is not able to detect it.
<script src="static/js/itemInventory.js"></script>
In browser console, i get an error
GET http://localhost:8080/admintool/selectItem/static/js/itemInventory.js net::ERR_ABORTED
How do i specify the location of this JS file properly ?
Are you sure, that the links are right? The link from the HTML to the JavaScript have to be:
../../static/js/itemInventory.js
Ivo

Source of LinkedIn script to create embeddable share widget

I am looking at the docs here:
https://developer.linkedin.com/plugins/share
and I have this:
the problem is that it's referencing a script file:
src="//platform.linkedin.com/in.js"
where/how do I get that script file? I need to include that script file in my project.
The script it in where you typed. platform.linkedin.com/in.js just remove the //. If you are wondering how to download it.
open the url platform.linkedin.com/in.js.
Right click on the code
Click on save page as
save where you want it

Find out redirected location of Javascript source

My HTML page has a <script> tag, which embeds a Javascript source file indirectly through a PHP script, e.g.
<script src="http://my.server.com/myjs.php?version=1"></script>
Based on the parameters to myjs.php (version only being an example) the PHP file simply sets the location
header("Location: version/1/mynewfile.js");
and the browser then automatically loads mynewfile.js.
Unfortunately mynuewfile.js needs to know the URL where it is loaded from, e.g. http://my.server.com/version/1/mynewfile.js.
Without using redirection, the globally executed code in mynewfile.js simply inspects the src property of the first element. Unfortunately, this only returns http://my.server.com/myjs.php?version=1. How can mynewjsfile.js get the real (redirected) URI of the script?
Of course, the PHP file could "simply" read the JS file and set a global variable to the JS file, but redirection is a really elegant solution and should be kept if possible.

get external file source (location) using javascript

I insert an external .js file from another domain. Like this:
<script src="http://externaldomain.com/file.js" type="text/javascript"></script>
file.js is always the same, but it can be placed in different domains,
so I need to know what is the source of the file.
For example: file.js is in domain1.com, domain2.com and domain3.com.
If someone insert it like this: <script src="http://domain1.com/file.js" type="text/javascript"></script>
I want to know that the file is inserted from domain1.com
How to do it?
There's no reliable way of achieving this. Basically inside this file you will need to parse the DOM, search for all the <script> tags and when you find the one that corresponds to the inclusion of this javascript file parse the src attribute to extract the domain.

Categories