I have a handlebars template in a public directory. I'd like to access the contents of this file with JavaScript. Here's the code for the script:
<script id="test_template" type="text/template" src="/templates/test.hbs"></script>
If I dump out the contents of the file inside the script tag (instead of using the "src" attribute) I can easily get at the contents, but then I won't get any caching.
Is it possible to get the contents of this file without using an AJAX call?
Is it possible to get the contents of this file without using an AJAX call?
No, you'll need to use an AJAX call.
Related
I have a file index.php and there is a main js script in its header.
I'm using Jquery load method to load other php file inside a div element:
$("#formHideinAjax").load('loginpass.php');
And as our friend explained here the main js file which had loaded in the header doesn't work in new imported file so I was forced to add the js file inside the loginpass.php too(Now I have two same js file, one in header and one in div that loads loginpass.php ! )
I know that this method of loading js file is not standard and sends more request to my server.
How can I fix this problem?
Well, it is explicitly said in $.load() documentation. I'm afraid you can't just paste <script></script> code into your DOM for browser to run it.
I'm afraid you have to send JavaScript code from your loginpass.php file (without <script></script> tags) and just eval it in your JavaScript
$("#formHideinAjax").load('loginpass.php', function () {
// eval JavaScript code from php file here
});
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.
I have included my json file like this
<script type="text/javascript" src="slideshows/1/myfile.json"></script>
And I wonder how I can now create a js object from this file?
If the json was defined inline, or defined in a .js-file it would already have a variable connected to it, but how does it work now that it is in a separate json file?
A JSON file is not a script. It is data.
You need to use a data access method, such as jQuery's $.getJSON() to get it.
Using script tags to import JSON in the page has only on useful use case, which is a way to prevent Cross-origin security issue from happening. We use it as JSONP in JavaScript by sending a callback attribute on the query-string:
<script type="text/javascript"">
window.getMyJSON = function(json){
};
</script>
<script type="text/javascript"
src="http://someotherdomain/slideshows/1/myfile.json?callback=getMyJSON">
</script>
BUT it seems here the JSON file is located just on the current domain, so as #Scimonster has pointed out you can simply achieve it using $.getJSON().
similiar How can I get the content of the file specified as the 'src' of a <script> tag? and Getting content of a script file using Javascript , I want to use script tag to load xml files for the use of a javascript code - only I need it to run locally.
so how can I access the content of the files loaded using script tag?
You can not achieve this one locally.
Loading the script will execute it and won't give you access to the src
so then the only option you would have is to load the file as an ajax call. But ajax calls don't work locally (due to security) .
i hav included below js file in my jsp
<script type="text/javascript" src='<s:url value="/script/customer.js?ver=1.2.3"/>'></script>
Jsp file contains hyperlink on which ajax call is made and response of that call contains again above code to include js file.
Ideally no call should be made for the same js file again they are cached by browser.Then i inspected in firebug and found out
below url is bit diefferent when i.e it includse extra parameter _=1360589976328. I do not want to make the call for the
same js file again if it has been included.(i can not remove from ajax response jsp because of some reason). I think problem
is this extra paramter, probably becoz of which call is made again.
Any idea why extra param is appended or double call is
made to include the js file when it already exists in browser?
http://localhost:8080/custModule/script/customer.js?ver=1.2.3?ver=4.5.8&_=1360589976328