I'm trying to use functions from external javascript files which are included in my html page with the "script" tag.
Javascript functions which are implemented directly in the html page seem to work without problems but the functions from the external javascript files aren't executed.
Is there a way to get these functions working?
If you want to load an external resource as for example a JavaScript file you need to intercept the call using a ResourceInterceptor and inject your file into the response.
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.
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) .
Is there a simple way to include a remote js file on the fly (in an onclick function)?
The "verify" onclick event for our ssl site seal requires a code include from the vendor. Trouble is, it slows loads times. So I extracted the function from their js and hosted it locally - but they change the request params from time to time so I have to manually edit the js.
It would be nicer if I could just include the remote file when someone clicks the site seal.
TIA.
If you are using jQuery you can use $.getScript
Description: Load a JavaScript file from the server using a GET HTTP request, then execute it.
You can use the createElement() and add it to the DOM. Please see the below document.
http://www.hunlock.com/blogs/Howto_Dynamically_Insert_Javascript_And_CSS
I'm loading user control through jQuery in my asp.net page.
User control contains JavaScript files, while loading the user control all my js load at one time which are dependent on each other and they tend to give error while all file load at one time. So I want that my JavaScript file to load synchronously one by one , as one file get completely loaded than next file should start loading .
Is there any way to set synchronously mode in JavaScript? or any JavaScript to set this? Any pointer or suggestion would be really helpful.
You should use jQuery.load() to load only a HTML fragment and not a full page with the scripts. jQuery use DOM structure of the loaded document to modify the DOM structure of the corresponding part of your page (controls).
In general you can use jQuery.ajax to load a script, but I recommend you to use the simplified form jQuery.getScript() instead. jQuery.getScript() can be used to load a JavaScript file from the server using a GET HTTP request and then execute it. Using success event handler you can do some action after the script are loaded.
JS files are loaded in the order you put them in your HTML code.
For example,
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>
You always need to load jQuery before jQuery UI (or UI will not be recognize since it uses the jQuery $ shortcut) so you must put the line with jQuery before the one with jQuery UI into your HTML.
And when your page is fully loaded, js will start thanks to window.onload, $(document).ready(function(){}); for jQuery or via the first command it will encounter.
JavaScript files always load synchronously. In fact, JavaScript always runs synchronously because it is single threaded.
My guess is that you need to work out which order to include the files so that it runs properly. You can use the window.onload event to run script once all of the JavaScript and images have been loaded.