javascript to external .js file - javascript

Can't figure out the proper syntax to place this javascript code into a .js file.
<script type='text/javascript' src='http://hostedusa3.whoson.com:8080/include.js?domain=www.yourdomain.com'></script>
<script type='text/javascript' >
if(typeof sWOTrackPage=='function')sWOTrackPage();
</script>

If you want only the inline code inside a separate file, then all you have to do is move the contents of the second script tag into a .js file. Then, set the src attribute to the second <script> to reference the new file's URL (relative or absolute).

external.js:
if(typeof sWOTrackPage=='function')sWOTrackPage();
in index.html:
<script type="text/javascript" src="external.js"></script>
If you want the other stuff in there to. Use your browser. Goto: http://hostedusa3.whoson.com:8080/include.js?domain=www.yourdomain.com copy that into external.js

Related

Why can't I execute JavaScript function inside the <script> tag that defines the .js source?

I am defining the source of a .js file and attempting to call a function from that file in the same tag, as follows:
<script type="text/javascript" src="jsFunctionTest.js">
testMethodCall();
</script>
The .js file just contains:
function testMethodCall(){
window.alert("Hello there");
}
This doesn't work, I don't see the alert.
However, if I change the tag to two tags, as below, then it works:
<script type="text/javascript" src="jsFunctionTest.js"></script>
<script type="text/javascript">
testMethodCall();
</script>
This seems pretty messy. Is there any reason the first one doesn't work?
script elements can have a src attribute or content, but not both. If they have both, the content is ignored (the content is considered "script documentation," not code).
You cannot register an external file and use the content in it, both at a time inside <script> tags. Only either one is allowed.

Call javascript from HTML

I have a HTML file with the content:
<script src="http://spelprogrammering.nu/simple.js">
function test()
{
//Function stuff
}
</script>
However, I'd like to write all my javascript (Function test) in a separate document (.js). How do I refer to, or call, this separate file so I get the same result as if the code was directly in the HTML?
I need the http://spelprogrammering.nu/simple.js to ease the graphics handling in function test.
You already have what you need in your source.
<script src="http://spelprogrammering.nu/simple.js"></script>
In the html section, refer to the file that contains your javascript, I'm assuming the html file is in the same folder as that containing your script.
<script src="script.js"></script>
If the script is in a different folder...
<script src="/path/to/script.js"></script>
<script src="http://spelprogrammering.nu/simple.js"></script>
<script src="main.js"></script>
Where main.js contains your function.
I think that you say that? in this case you need to make difrent script calling your .js
<code>
<script src="http://spelprogrammering.nu/simple.js"></script>
<script src="http://spelprogrammering.nu/other.js"></script>
<script src="http://spelprogrammering.nu/other2.js"></script>

Using ResolveClientUrl() in external JavaScript files

I found that ResolveClientUrl() works as expected when JavaScript is embedded in ASPX page, however it doesn't resolve anything when external script is referenced in ASPX like this:
<script src="../Javascript/sessionManagement.js" type="text/javascript"></script>
Is there a way to make ResolveClientUrl() work in external JS files? I was expected that it would because this file IS included in ASPX page but this is not the case.
I found a rather ugly workaround - to include external JS file in ASPX page like this:
<script type="text/javascript" language="jscript">
<!--#include file="../Javascript/sessionManagement.js"-->
</script>
It works (ResolveClientUrl() actually resolves path) but I've never seen external JS files referenced like that and am not sure this is the way to go.
<script type="text/javascript">
var myUrl = '<%= ResolveClientUrl("MyURL") %>';
</script>
<script type="text/javascript" src="../JavaScript/sssionManagement.js"></script>
and in your sessionManagement.js, you can simply use the myUrl variable above

Calling external function from jquery (document).ready?

I'm trying to call a function in an external file from within a HTML page using the document.ready jquery functionality. Below is the example of code from my HTML, but it does not execute the function with the code I've written.
<script type="text/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="_js/script.js">
//<![CDATA[
$(document).ready(function(){
// What do I run here to grab external file function?
extFunction();
});
//]]/>
Example of function from external file:
function extFunction(){
alert("ALERTED!");
};
<script type="text/javascript" src="_js/script.js">
You cannot have a src attribute on a <script> tag and also have JavaScript code inside the tag. Once the src attribute is seen by the browser, it does not execute anything within the tag. Please make two separate tags...
<script type="text/javascript" src="_js/script.js"></script>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
// What do I run here to grab external file function?
extFunction();
});
//]]/>
</script>
You cannot have body(content) and src for a script element
<script type="text/javascript" src="_js/script.js"></script>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
// What do I run here to grab external file function?
extFunction();
});
//]]/>
</script>
you want to call function from a php file or js file ??
if you want to call a function of another .js file just include that file in your .js file and if you want to call a php function then use ajax.

cant get jquery to work with php file

I have a linux webserver that has /var/www configured in the Apache2.conf file as the DocumentRoot. Next I have my jquery core file located at /var/www/js/jQuery_v1.4.2.js (a central location for all my websites to access.
The .php index file is located at /var/www/AOI/aoiparse.php, where the aoifunctions.js file is also located. My <head> tag looks as follows:
<script type='text/javascript' scr='/js/jQuery_v1.4.2.js'></script>
<script type='text/javascript' scr='aoifunctions.js'></script>
my aoifunctions.js file has the following in it in order to verify that the script link works:
$(document).ready(function(){
alert("hello");
});
My problem is that I cannot get the alert() to work. I'm not getting an error message so I do not know where the problem is.
You can try <script type='text/javascript' src='../js/jQuery_v1.4.2.js'></script>
(The attribute is src, not scr, plus your path was incorrect)
put the jquery file in /var/www/AOI/js/ and then
remove the first /
<script type='text/javascript' src='js/jQuery_v1.4.2.js'></script>
so it lies in a subdirectory to your main page
or use this if you want to leave it where it is:
<script type='text/javascript' src='../js/jQuery_v1.4.2.js'></script>
You misspelled "src" in your <script> tags. It's short for SouRCe.
<script type='text/javascript' src='/js/jQuery_v1.4.2.js'></script>
<script type='text/javascript' src='aoifunctions.js'></script>
If you used Firebug you'd have been able to easily tell that the scripts weren't loaded.

Categories