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>
Related
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.
HI i am facing some problem while calling function from index.html on load event
it return error readjson is not defined in console.
index.html
<script type="text/javascript">
$(window).load(function () {
readjson('a','s');
});
</script>
main.js file
<script type="text/javascript">
function readjson(val1,val2){
//some code
}
</script>
Can anyone tell me why it is not able to call, i have link main.js file to index.html
JavaScript files shouldn't include any HTML.
Remove <script type="text/javascript"> and </script> from main.js.
You should see an error when that file is loaded (along the lines of Unexpected token <).
Call it like this inside the js file without the "script" tag :
function readjson(val1,val2){
//some code
}
In index.html you need the "script"
And follow the advice given in the comments, always include first the .js file and then the function in the index.html
Did you add jquery link properly??
If not, you should add jquery link for $(window) to be called. such as..
<script src="http://code.jquery.com/jquery-latest.js"></script>
If you actually have two script declarations as you are showing here the the window load will fire first. It then look for your function, which has not been created yet. That's why you are getting undefined.
You could have one script declaration on the page and place your function in the load function.
<script>
$(window).load(function () {
readjson('a','s');
function readjson(val1,val2){
//some code
}
});
</script>
Remove tags from main.js:
<script type="text/javascript"> and </script>
and remember to include jquery and main.js with:
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js"></script>
<script src="test.js"></script>
I have to use a plugin which bundles js files inside html files (gadgets). For one use case I need to drop and re-instantiate a gadget to run updated code.
So say I have foo.html:
<html>
<head>
<script src="foo.js"></script>
</head>
<body>...</body>
</html>
and foo.js which is the file being injected into my actual document's head:
alert("hello");
Problem is I can only cachebust the html file dynamically and declare my gadget as foo.html?x=123, but the JS file I'm after will still be foo.js so the browser will not re-run it.
Question:
Once a <script> tag is inserted into the document and run, is there any way to run it again without using a module-loader or eval?
Thanks!
You could wrap your code in your <script> tags in a function then call your function. This will allow you to call your code to be called multiple times. Like this:
<script>
function loaded(){
// JavaScript here
}
loaded();
</script>
</body>
My problem is, that I have created a js file. In this file are some text defined.
channel = {
categorie_one: "Hauptsender",
categorie_two: "Spartensender",
categorie_three: "Regionalsender"
}
Now I want to embed categorie_one in a other js file. That I'm doing with that code:
channel.categorie_one;
But it shows in console: Cannot read property 'categorie_one' of undefined, logical I have linked the file...
Im including the js files in the index.html
<script src="javascripts/default.js" type="text/javascript" rel="javascript"></script>
<script src="javascripts/resources.default.js" type="text/javascript"></script>
In the default.js I have a methode to load the site.
function ChannelLoad(listview) {
//there should be cateogrie_one
}
could you help me pls. Thanks in advance
add a semicolon to your variable definition:
channel = {
categorie_one: "Hauptsender",
categorie_two: "Spartensender",
categorie_three: "Regionalsender"
};
syntactically your variable definition is a statement; if placed in a row with other statements (i assume that's where it will end up after inclusion of your js files), they have to be separated by semicolons.
In your html file include the scripts in the right order:
<script type="text/javascript" src="defines_object.js"></script>
<script type="text/javascript" src="uses_property.js"></script>
Make sure the object isn't defined in a function or the scope of the object will be limited to that function.
If you defined the channel variable in the resources.default.js file and you are trying to access it in the default.js file, you have to reverse the order in which you link the JS files.
Try:
<script src="javascripts/resources.default.js" type="text/javascript"></script>
<script src="javascripts/default.js" type="text/javascript" rel="javascript"></script>
This code should work.
The only thing you must be aware of is the order you include the files, all files will be executed in the order of their includes, so you should include first the file declaring the object, containing
channel = {
categorie_one: "Hauptsender",
categorie_two: "Spartensender",
categorie_three: "Regionalsender"
}
then include the second file, using channel.categorie_one;
If this still doesn't work, please post your whole code, there's probably a scope issue (channel is declared in a local scope)
You should include your scripts in the right order:
<script type="text/javascript" src="defines_object.js"></script>
<script type="text/javascript" src="uses_property.js"></script>
The code in the external file is
var testing = {
bugtest: function() {
alert('No Bugs Here');
}
}
In the php file I am using
<script type="text/javascript" src="externalScript.js">
testing.bugtest();
</script>
But this will not work why?
if I call the function in the external fil it works
var testing = {
bugtest: function() {
alert('No Bugs Here');
}
}
testing.bugtest()
this will work but this not what I want it to do I want to be able to call the function in the main file? What would the cause of this problem be?
You can not use src attribute and the text node with script elements.
They must be exclusive, e.g. an element each.
So your HTML would look something like...
<script type="text/javascript" src="externalScript.js"></script>
<script type="text/javascript">
testing.bugtest();
</script>
This
<script type="text/javascript" src="externalScript.js">
testing.bugtest();
</script>
is wrong. You can either specify a src, or run inline code.