jQuery call function from external JS file - javascript

I want to call a function from my master .js file - from within my php page. Is this possible?
<script type="text/javascript">
functionName(); //calling function from master.js file
</script>
Doesn't seem to work.

It should be as long as the external script is loaded first and "functionName()" is attached to the global scope

Related

How to simplify my js code without loading the js file twice?

I hava many jsp page using the common function in body's onload and onunload event when using ocx control.So I extract the common function in one js file call common.js and import the js file in the jsp page.But I find the sometimes the jsp page could not execute the function in js file.Finally,I solve the problem in two way but I think there must have the other simple way?How to simplify my js code?
write all the js function in each jsp,it works well
in the each jsp file,includ the common.js,and in the jsp file the script call the function before load the common.js,but I think it have already be loaded two times
//init(),asynclogin_onclick(),logout_onclick() function is in the common.js file
<head>
<script type="text/javascript" src="/js/common.js"></script>
</head>
<body onload="init()" onunload="logout_onclick()">
$(function(){
$.getScript("/js/common.js")
.done(function(script,textStatus ) {
asynclogin_onclick();
});
});
I try to write the code like this,but it could not work.
$(function(){
asynclogin_onclick();
});

Can an inserted <script> tag be run multiple times without using eval in JavaScript?

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>

Can't call global javascript functions from android phone

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>

how to pass a variable through .js file

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.

call js function in html from .js file

Ok, I know how to register my js file in my html and call functions in the .js from my html file. Here I am trying to do the opposite , I have a function in the .js file and I want to call a function that is in my html file. How do I go about registering the html (it's actually .aspx ) but that really shouldn't matter for the scope of this question.
Just make sure that the JavaScript function in your HTML is defined on the page before the code in the .js include calls it.
Simply include your external script to the page at the end your body tag or at any place after your function declaration.
Aspx page:
<body>
content content
<script>
/* Function to be called */
function s(){
alert('Hello World!');
}
</script>
<script src="myJS.js"></script>
</body>
myJS.js:
/* Call a page function */
s();
By the time your external file is loaded your function s() will be declared.

Categories