Suppose I have a site at www.somewhere.com that loads a javascript file called Test.js hosted at www.aplace.com. From within Test.js, how can I get the url www.aplace.com?
When I use window.location, I get the url of the site (somewhere.com) instead of the url of Test.js.
The reason I am asking is because I need to make a post request from Test.js to www.aplace.com/dirA/dirB/dirC/Test.js to www.aplace.com/dirD but I can't hardcode the URL.
They should be in <script> in src='{THE_URL}', perhaps try scraping if API is not present.
Related
I have a JavaScript file called newsletter_init.js and it makes an Ajax request.
I add this JS file to every .php on my project and the issue here.
For example I use newsletter_init.js in my index.php and a URL for the ajax request will be res/ajax/request_newsletter.php.
Ok, that's working but when I add the same JS file on another directory for example adding newsletter_init.js on a index.php but placed on blog folder the URL will be invalid as it will search for ../res/ajax/request_newsletter.php instead of res/ajax/request_newsletter.php.
File structure about the scope:
index.php
blog > index.php
I have another way to do this:
Add content of newsletter_init.js on every file and change url but this is not a good way to approach this.
I have tried to use __DIR___ in PHP but i don't understand how to use it in this situation.
I have tried $(location).attr(pathname); in jQuery but it return the fild name also for example localhost/homepage/index
I can't use a direct url as i will change from localhost to my site so, i need a dynamic way.
Just ensure your path is relative to the root directory.
/res/ajax/request_newsletter.php
Not:
res/ajax/request_newsletter.php
If you don't add the first slash, it will try to find that path starting in the directory where you are making the call (i.e. wherever your current PHP script is running).
The problem is that the following external javascript file is not executing:
<script src="//www.google-analytics.com/cx/api.js?experiment=YOUR_EXPERIMENT_ID"></script>
When I access the URL directly it downloads the file as f.txt
And when I copy/paste the content from the file then it's fully functional js and working as it should: creating cxApi object.
Got the snippet from: https://developers.google.com/analytics/solutions/experiments-client-side
Presumably you are testing using a local file.
i.e. the URL in your browser's address bar is something like file:///Users/you/Desktop/test.html
The relative URL //www.google-analytics.com/cx/api.js?experiment=YOUR_EXPERIMENT_ID then resolves to file://www.google-analytics.com/cx/api.js?experiment=YOUR_EXPERIMENT_ID, which does not exist.
You may see an error in the Console of your browser's developer tools. You should see one in the Network tab.
Test using a web server. Access your pages over HTTP
Then you will have something like http://localhost/test.html, and the URL will resolve to http://www.google-analytics.com/cx/api.js?experiment=YOUR_EXPERIMENT_ID, which does exist.
My website is in /var/www/html.
I have a few files (PHP and Javascript) that are one level up, in /var/www.
I'm able to access the PHP file for my MySQL login credentials, but I can't access the Javascript file. In the <head> of my Login document, this is how I have it:
<script src="../sha256.js"></script>
This worked fine until I moved the sha256.js file. But now, when I try to login, the document can't find the file.
Can I access a Javacsript file that is out of the document root?
No.
If it isn't under the Document Root then it doesn't have a URL1.
If it doesn't have a URL then the browser can't request it.
Given the URL http://example.com/ and the relative URL from it ../foo the browser will delete a 'directory' off the end of the URL for each ../. If there aren't any, then it will ignore them. Thus it resolves to http://example.com/foo.
I'm able to access the PHP file for my MySQL login credentials
This is, presumably, server side code which deals with the server's file system and not with URLs.
1This is a simplification. There are other ways (alias, mod_rewrite, etc) to give a file a URL, but for your purposes, moving the file under the Document Root is the simplest solution.
I have the following code:
<script>
var fileContent;
$.ajax({
url : "text.txt",
dataType: "text",
...
It loads the text from a .txt file in order to obtain the data. If the text.txt is on the same path as the html code, it loads the data. However, if I type for example (placing the file in a different folder):
url: "../../../files/text.txt"
It does not allow me to obtain the file. Any ideas of how to do it or how to implement it without changing the code in a significant way? Thanks!
There are three possible causes of this:
You are using HTTP
You are using HTTP and the path you are trying to access is not exposed by your web server. (You cannot access files above the directory root by default).
You need to give the file a URL on your web server and request that URL.
You are using local files
Different browsers have different security restrictions for Ajax on local files.
You can get this problem if the file is in a directory above the HTML document (some browsers only allow you to access files in the same or a lower directory).
You can resolve this by using HTTP. Ajax in general works very poorly without HTTP.
You simply have the URL wrong
Correct the URL.
I recently read on Meta about some browser not flushing their cache even after reading a script url of this form
myscript.js?v=1234
so to go around the problem i am thinking about implementing a solution i also read but without any details given to it. something like myscript-1234.js and reroute to the actual correct file, but i have a doubt now.
Should i rewrite that url to myscript.js or to myscript.js?v=1234 ? I am actually confused as to how it even going to make a difference to have a rewriting.
Your rewriting should not redirect to any other URL (which would the be fetched by the browser), but should be "internal" on your server.
What I mean is that when receiving a request for "myscript-1234.js", your server should instead serve the content of the myscript.js file ; which will always be the last version.
In the end :
For the client the is a different URL each time you update the file on the server : myscript-1234.js, myscript-1235.js, myscript-1236.js, ...
This is why the browser will try to re-fetch the file from the server : as it's not the same name, it will not have the file in cache
But, for the server, there is always one and only one file : myscript.js
And you're using some "rewrite" rule so thr server just removes the -XYZ portion of the file name before trying to read it from disk.