Include external JS file and setting variable - javascript

I need to include an external JS-file with
<script src="/IframeHelper.js" type="text/javascript"></script>
But I need to set a variable and I tried somthing like this:
<script src="/IframeHelper.js" type="text/javascript">var $Vari = 'Para';</script>
And inside the "IFrameHelper.js" something like this:
var $Site = 'Rootfolder' + $Vari;
But you know...this doesn´t work. But how can I do this? It´s a little bit like calling a script with parameters. But such a parameter can be a JSON-String oder "long text".

As per my understanding you need to include IframeHelper.js file after you declare your variable.
<script type="text/javascript">var $Vari = 'Para';</script>
<script src="/IframeHelper.js" type="text/javascript"></script>

Related

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

can't embed some text in js file

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>

how to pass value to a js file

I know this question are asked hundreds of times :( but I just want to learn more :).
My question is simple, can I pass a value to a js file like this, if not, how ?
<script type="text/javascript" src="./js/create.js?method=create"></script>
Yes, you notice that I have a parameter method=create which I want to use in my create.js.
I know in jquery ajax, we have an easy way, but you must notice that the ajax method is in included in the js file, how could I pass a parameter to the js file itself ?
Any answer is welcome :)
Thanks.
This works perfect for me:
<script src="js/script.js" id="myScript" data-url="my_variable"></script>
inside the script.js file:
var myUrl = document.getElementById("myScript").getAttribute( "data-url" );
"myUrl = my_variable" inside the code
you can use myUrl everywhere.
Not really, no. What you can do is this:
<script type="text/javascript">
var method = "create";
</script>
<script type="text/javascript" src="./js/create.js"></script>
Another way is to only define functions inside your javascript file, and then invoke after it has loaded.
<script type="text/javascript" src="./js/create.js"></script>
<script type="text/javascript">
runMyLoadedCode("create");
</script>
Third way is belying my first simplistic answer: access the script tag itself and parse it. You can see here how to access the tag that has loaded your script; take its src value and cut it up to locate your method=create.

Accessing variables in another script

I have a JS
function check(obj) {
for( i=0; i<obj.elements.length; i++){
if(obj.elements[i].type=="checkbox"&&obj.elements[i].checked){
if(confirm(onSubmitMessage)){
return true;
}
else{
return false;
}
}
}
alert(alertMessage);
return false;
}
It's called from jsp page like this:
<script src="/TestAppChanged/check.js" type="text/javascript">
var onSubmitMessage = '"<bean:message key="body.onsubmit.delete"/>"';
var alertMessage = '"<bean:message key="body.alert.delete"/>"';
</script>
...
<form action="MyAction" method="POST"
onsubmit="return check(this)">
The problem is in that it doesn't see these glabal variables: onSubmitMessage and alertMessage. I thought that the problem was in the way that these things are set and changed it's values to usual strings like "qwe" but it didn't work again. So script in it's body simply doesn't see these variables. And the question is how to get them from script?
The <script> tag is used either to load an external file, like <script src="path/to/file.js"></script> or to define JS code inside the tag, like:
<script type="text/javascript">
// Your JS code.
</script>
You can't have both on the same tag. Thus, split your code:
<script src="/TestAppChanged/check.js" type="text/javascript"></script>
<script type="text/javascript">
var onSubmitMessage = '"<bean:message key="body.onsubmit.delete"/>"';
var alertMessage = '"<bean:message key="body.alert.delete"/>"';
</script>
A block between <script src=""></script> parsed when the browser don't support javascript. You need another <script> block, and put in this your variables.
It doesn't see the globar variabes because you declare a source in the script tag.
You should remove the global variables and declare them before the code of the function, in the js page, and then simply include the page like this:
<script src="/TestAppChanged/check.js" type="text/javascript"></script>
You should have two script tags. One for the variables. And then another one below it, linking to your javascript file.
You can set contents in tag OR set the src attribute.
It should be:
<script type="text/javascript">
var onSubmitMessage = '"<bean:message key="body.onsubmit.delete"/>"';
var alertMessage = '"<bean:message key="body.alert.delete"/>"';
</script>
<script src="/TestAppChanged/check.js" type="text/javascript"></script>
Don't mix the src attribute and code within the tag. Use two tags.

Categories