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>
Related
I have a button that is supposed to have onclick="submitItem()" but the function does not work when it is created in an external javascript file (app.js).
<button class="btn" type="submit" onclick="submitItem()">Save</button>
When the function is placed in between script tags, then it works, but that's not what I want.
<script src="./js/app.js" type="module"></script>
<script>
function submitItem() {
alert("works");
}
</script>
But this does not work:
app.js
function submitItem() {
alert("does not work");
}
What can I do?
It isn't a module. Don't claim it is. Remove type="module".
(Top-level variables, including those created by function declarations, are scoped to the module and not as globals in ES6 module scripts).
I am trying to accomplish something similar to: Calling a function from one JavaScript file which requires another
Essentially, I have index.php, which is the main file for the home page. Within the index file, I am calling a javascript function from another js file.
<script src="js/data.js"></script>
<script>(go_call())</script>
within data.js:
function go_call(){
alert('working');
}
I get the following error: Uncaught ReferenceError: go_call is not defined
UPDATE 1:
My original code is working fine in IE, but this error I have occurs in google chrome.
A function cannot be called unless it was defined in the same file or one loaded before the attempt to call it.
A function cannot be called unless it is in the same or greater scope then the one trying to call it.
It should work like this:
1) within data.js
function go_call(){
alert('working');
}
2) Within file2.js
function clickedTheButton() {
go_call();
}
3) Html File:
<html>
<head>
</head>
<body>
<button onclick="clickedTheButton()">Click me</button>
<script type="text/javascript" src="data.js"></script>
<script type="text/javascript" src="file2.js"></script>
</body>
</html>
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>
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.
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