So I have a site at http://mysite.com/ and I have a Javascript file on a CDN, the Javascript file could be at http://cdn1.mysite.com/ or http://cdn2.mysite.com/, I need to get the value of the hostname the javascript file is on. Is there any way to do this? I cannot modify the embed code of the javascript file to add a custom ID or add an additional variable on mainsite.com.
The reasons why I need this are far too complicated for one post =)
You can get all the scripts you are loading in your page and search by name for the right one
Here is your example
If you know the index of the embedded <script> inside document.getElementsByTagName('script') you may read the src-attribute of the <script> and extract the hostname from that src.
Related
Good morning, I was reviewing other topics on the site but I could not find a way to solve my problem, at the end I will include some of the other topics on Stack that I was reviewing.
I am trying to create an html file that can read url parameter to invoke that url in an iframe in such HTML,
e.g.: example.com/index.html?url=https://stackoverflow.com
and this way I could load it from my site using an extractor I have.
This problem arises because I have several locations for files and I need to simplify the access to them but at the same time to have all of them in a list. When I tried to use some themes in Stack to find my solution it only caused a massive load of iframes opening and loading that never finished until it reached the limit of ram available on the server and in other cases it didn't read the url set in the parameter
Just in case, I clarify what I am looking for:
I have my site examplea.com and I want to display files through b.example.com?url=sitebackupA and so on, I just need to achieve a url parameter with javascript, thank you very much!
These are the other issues where I was checking and only the TypeForm was something close to what I need
Pass Incoming URL Parameters to iframe src with Javascript
Using Javascript for iFrame src URL
Changing iframe src with Javascript
var url = new URL(location).searchParams.get('url');
document.querySelector("iframe").src=url;
<iframe></iframe>
Is it possible to download a script file using the script tag like this
<script src="http://webserver.com/script.js"></script>
and then be able to read the loaded file content with JavaScript, like this for example:
var scriptContent = window.scripts[0].content;
What i came to notice is that the loaded JavaScript file contents is not accessible by other JavaScript files or am i wrong ?
No, it is not possible to do that.
The source code of a script loaded via src is not exposed via any API made available to JavaScript running in the page.
You could read the value of src and then fetch it using XMLHttpRequest.
The source code of specific functions may be available by calling toString() on them.
As the title states. I am looking to get the extension of the file, even if it is hidden via htaccess, for example:
.../whatever/index
Imagine it was a php file, is it possible to know that and extract it via JavaScript or jQuery?
Thanks in advance, can't find it anywhere. All I can find is people trying to actually hide the extension.
To achieve this you need some sever-side coding where you can request which file is behind the given path, so when you pass "/whatever/index" you server should returns 'whatever.php' and from that information you could extract the file extension.
Javascript itself doesn't know anything about what/how things are organized on the backend
Is it possible to get the Javascript server path?
For example, I have a javascript file that is in a directory, and in that directory there are some php scripts that I want to "post" to.
When I include the javascript file in my home page
<script type="text/javascript" src="assets/js/some.js"></script>
I can't access say a file like /post.php that resides in the assets/js/ directory using relative paths since the javascript is not being "run" in the directory that the home page is in.
Obviously, I can specify the path, but I was trying to think of a more robust way.
I know you can get the "script" tags and get the url of the file, and I could probably work from there, but I didn't know if such a function exists, that would just tell you.
Thank you!
Create a script that is served from bla.php and place it before your some.js
<script type="text/javascript" src="bla.php"></script>
Inside bla.php, render a javascript class and store your data in it.
i.e.
function MyConstants() {
this.HELLO = "hello";
this.POST_PATH = "/etc.php";
}
You can access the constants (or anything else) via:
var consts = new MyConstants();
alert(consts.HELLO);
Hope this helps! :-)
You can try to use a cross-browser stack tracer to get the current file name
however there are many cases where your idea this might break, For example if you add a JS cacher that complies all necessary JS files into one and compresses them.
I would say that a smarter approach is to have a principle in place with which you can figure out the urls for your php skripts, like RubyOnRails has routes
I don't know which title I should use for this question.
I have a webpage (e.g. index.html) which contains flash content, url:
http://www.abc.com/travel/sg/traffic/
After I finish the webpage and upload onto the staging server, the customers said that they may need to use different domain to go to the site, e.g.
http://sg.travel.com/
The images or hyperlink do not work because of this. To handle this, I try to use the base tag as follows:
<base href="http://www.abc.com/travel/sg/traffic/" />
The images and hyperlink work. However, flash file cannot call javascript afterwards.
I would like to know how I can fix the problem.
Thanks.
Are you using relative links in the flash file? Try passing the url as a parameter and prepend the links in the flash file with them. You may also need to set up a proper crossdomain.xml to allow your flash to access other domains.
if images are the same domain as the swf, you can use relative paths.
try with
"/images/foo.png" instead of "http://www.abc.com/travel/sg/traffic/images/foo.png"