I am passing some variable from my gwt code to the jsp file. It is something like this
<html>
<head>
<script type="text/javascript">
alert("Inside the jsp file");
var criticalPath = window.top.criticalPath;
</script>
</head>
<body>
<script type="text/javascript" src="./js/flow.js"></script>
</body>
</html>
What i want is to pass the criticalPath variable to the flow.js file. I know if, flow.js would be a method I could have pass the variable, but I don't want to do that way. Is there any way to avoid the method and pass the variable to flow.js or can I call the window.top.criticalPath inside the flow.js?
You didn't place the variable criticalPath in any specific scope, which means it is a global variable, and hence it will be accessible from any js file (as long as they are loaded after criticalPath declaration.
Related
Why it is necessary to first include an external js file and call any functiont later on with new script tags ?
I am testing with the code:
test.html
<html>
<head>
</head>
<body>
<script src=test.js>test();</script>
</body>
</html>
test.js
function test(){
alert(1);
}
It does not show an alert popup.
But when I include test.js separately either in body or head with the code:
test.html
<html>
<head>
</head>
<body>
<script src=test.js></script>
<script>test();</script>
</body>
</html>
It does show a pop-up indeed. Does it have anything to do with the HTML parser? I am not even getting a ReferenceError displayed in the browser console so test has a reference but it is not executing.
Code in the global namespace must be loaded in the order such that executed code must first be defined.
For example, if a.js had...
var a = function() {
alert('a');
}
...and b.js had...
a()
...then you wouldn't want to include b.js before a.js, or a() won't be available.
For above,
<script src=test.js></script>
<script>test();</script>
again it is the same way: First include file, then run its contents.
this happens because when you specify a src attribute you told the browser not to look for javascript inside this tag but instead from an external one
I have a global file functions.js with global javascript functions which are called from other javascripts. Why is not this working on my android phone?
Functions are defined like this in functions.js
function function_name(){
alert("test");
}
And called like in the other javascript files.
function_name();
Is there another way to deal with global javascript functions to get it working on android?
It was a function defined with a parameter like:
function example(parameter = ""){
...code...
}
which made it impossible to call functions.
If you want to call a global function that is delcared in a .js file, from a different HTML file, then you must include the .js file in the <script> tag
/* Your code.js should look like: */
function function_name(){
alert('test');
}
then
<!-- and your HTML file should look like -->
<HTML>
<HEAD>
<SCRIPT SRC='code.js'></SCRIPT>
<script>
// you may call your globar function here:
function_name();
</scirpt>
</HEAD>
<BODY>
<script>
//you can also call your function anywhere here:
function_name();
</script>
</BODY>
</HTML>
I am trying to pass a variable from page 1, that has a javascript that uses a .js file for the submit button. I want to take that variable and pass through the .js file to a frames page that needs to output that variable again.
Here is the "button" I am trying to pass the variable (located in the .js file)
window.location = SC.baseUrl + '/reservation/' + reservation.token;
The variable I am trying to pass is called $event_name
The variable is being queries from mySql and I am using codeigniter. When the page loads, the query filles in the event name, which I am then trying to pass that variable into another page through this .js script.
Any ideas?
I would highly recommend looking into using browser cookies.
You can't pass variables between 2 separate JS files. What you could do is have functions in both files that return something. For example:
JS File 1
function myFunction1 (arg1) {
return arg1;
}
JS File 2
function myFunction2 () {
return myFunction1 ("foo"); //myFunction1 is in a separate JS file
}
alert(myFunction2()); //Throws alert message that says "foo"
HTML
<!DOCTYPE Html />
<html>
<head>
<title>
</title>
</head>
<body>
<script type="text/javascript" src="theJS2.js"></script>
<script type="text/javascript" src="theJS.js"></script>
</body>
</html>
Both files would have to be loaded into the same page, of course. KIND OF the same concept of using jQuery or some other JS Library with your code.
I'm writing java script code in script tag in head like:
<head>
<script language="javascript">
object o = new object({....});
</script>
</head>
and trying to use object a in body tag
<body>
<script>
alert(o.value);
</script>
</body>
how can i access object from body???
is their any alternatives?
<head>
<script language="javascript">
var o = new Object();
o.value="a"
</script>
</head>
<body>
<script>
$(document).ready(function()
{
alert(o.value)
});
</script>
</body>
In this case var a is accessible in complete application, but one thing you need to make sure if you are using external JS files then it must be loaded when you using the variable. try onload function to assure JS is loaded and ready to use in body:
window.onload = function ()
{
alert(a);
}
Since your variable is declared outside any functions it can be accessed from anywhere in your document from the same script block or from a seperate script block like in your example. it can even be accessed from html event-attributes like this (note that its better to attach events to html elements using using js):
<button onclick="alert(a);">Click this button to open an alert!</button>
I have two files screen.html and db_fun.js.I have declared a variable at just the beginning as follows:
db_fun.js
var name = "abc";
now i tried to access this variable in the screen.html file as follows
screen.html
<html>
<body>
<form name = "screen" action = "db_fun.js">
<p> <script>document.write(name);</script> </p>
</form>
</body>
<script src="db_fun.js" type="text/javascript" />
</html>
it doesn't print nethn. Y so?
add this to the head
<script type="text/javascript" src="db_fun.js"></script>
I think that you should move
<script src="db_fun.js" type="text/javascript" />
In the head of the page. I.e. before document.write.
Because in the moment where you are trying to print 'name', the variable is still not created. Also I think that it is not a very good practice to use document.write to add content to your page. Try using jQuery or some other library. For example:
$(document).ready(function() {
$("p").html(name);
});
That's because you are inserting your JavaScript in the page footer. You have to firstly load your javascript file into your web site (in the head section for example) and then use your variable.