I have two javascript files and one html page. i want to include a js file to another js file but i dont know how to include js file to another one.
anyone can please help me?
following is my code
html code:
<html lang="en">
<head>
<script src="main.js" ></script>
</head>
<body onload="show()">
</body> </head>
</html>
following are the javascript files:
1.js
var x=10;
function show()
{
setvalue(x)
}
2.js
var D=;
function setvalue(x)
{
D = x;
alert(D);
}
if it's just a matter of accessing the functions and objects in the first javascript file just do something like this:
<script src="main1.js"></script>
<script src="main2.js"></script>
this way any function in main1.js will be available in main2.js
Related
I have two file:
html2canvas.js
function html2canvas(){
}
myid_print.js
(function ($) {
$(document).ready(function() {
//I want to call html2canvas function here
html2canvas();
});
})(jQuery);
I already included both files in my html but after running the code above, the console shows an error saying:
Uncaught ReferenceError: html2canvas is not defined
How will I call the function from inside my second file?
Try implementing your Client side code as :
<head>
....
<script src="html2canvas.js" type="text/javascript"></script>
<script src="myid_print.js" type="text/javascript"></script>
....
<head>
<body>
...
<script type="text/javascript">
function_from_myid_print();
</script>
...
</body>
Inside which you can call html2canvas();
This will surely help you.
For more details refer links:
https://stackoverflow.com/a/3809896/4763053 and https://stackoverflow.com/a/25963012/4763053
Try put your JS script at the bottom of your HTML page.
i made a html page with this coding
<html>
<head>
<script src="../a.js">
var u=document.URL; var i='t4527878445'; m_web(u,i);
</script>
</head>
<body>
</body>
</html>
In a.js i have this code
function m_web(u,i) {
alert('l');
alert('u');
}
but my webpage is unable to call this function which is coded in an external file. i am not getting any alert with this. i don't know what is problem. plz tell me simple solution for this.
thanx in advance
A single <script> tag can link to an external resource using the src attribute OR contain inline JavaScript code, but it can't do both at the same time. If you specify a src attribute any content between the <script src="foo.js"> tag and the </script> tag is ignored.
Since you want to load the external JS file, and then execute some JavaScript code, you'll need two separate tags to do so:
<script src="../a.js"></script>
<script>
// your code
</script>
plz write your code like below
<script src="../a.js"></script> </script>
//^^^^^^^^^^^^^^^^^^ first close script tag of linked js file
<script type="text/javascript">// then call your inline jscode
var u=document.URL; var i='t4527878445'; m_web(u,i);
</script>
Try
you are not closing script tag
<script src="../a.js"></script>
^//added closing script tag
<script>
var u=document.URL; var i='t4527878445'; m_web(u,i);
</script>
to alert what you pass use
function m_web(u,i) {
alert(i);
alert(u);
}
I am trying to call a function written in one JavaScript file from another JavaScript file. I have the following code, but it doesn't work:
My HTML file
<script type="text/javascript" src="js1.js"></script>
<script type="text/javascript" src="js2.js"></script>
<script language="javascript">
js1();
</script>
js1.js
function js1()
{
alert("Hello from js1");
js2();
}
js2.js
function js2()
{
alert("Hello from js2");
}
What can I do?
Try changing the order
<script type="text/javascript" src="js2.js"></script>
<script type="text/javascript" src="js1.js"></script>
<script language="javascript">
js1();
</script>
Because you call js2(); inside js1.js, so the script js2.js should be executed before.
In your case, i think it should still work without changing orders like this because you call js2(); inside a function. When this script is executed:
function js1()
{
alert("Hello from js1");
js2();
}
Even the js2.js is not executed yet, but you do not actually call js2(); at this time.
Just try it to see if it works.
I'm going to assume that's your entire HTML page.
In order to have those scripts run, you need to have those JavaScript files in the same folder as your webpage, and to actually have a proper HTML page!
In your HTML page, you need to include the references to your js1 and js2 files in either the head or body, and include the script you've written in the HTML page itself in the body so that it'll execute when it's loaded:
<!DOCTYPE html>
<!-- ^ Declaring this DOCTYPE means this is a HTML5 page. -->
<html>
<head>
<!-- This will load your scripts into the document. -->
<script src="js1.js"></script>
<script src="js2.js"></script>
<!--
In a HTML5 page, you don't need to include the
'type="text/javascript"' attribute on script tags.
They're treated as having that by default, unless you say otherwise.
-->
</head>
<body>
<!--
You could also include your scripts here, but I'll
just leave these commented out since they're already included.
<script src="js1.js"></script>
<script src="js2.js"></script>
-->
<script>
js1();
</script>
<!--
You don't need 'language="javascript"' on a script tag.
Use the type attribute, or nothing in a HTML5 page.
-->
</body>
</html>
I'm trying to re-create this locally: http://jsfiddle.net/janpetras/wzBu8/
Granted, it shouldn't be that complicated, but I can't get it to work.
This is the code:
$(function(){
var moneyEarned,yearlySalary,secondSalary;
$("#startEarning").click(function(){
moneyEarned = 0;
var hourlySalary = $("#hourlySalary").val();
if(hourlySalary.length > 0) {
secondSalary = hourlySalary / 3600;
} else {
yearlySalary = $("#yearlySalary").val();
secondSalary = yearlySalary / 7200000;
}
setInterval(updateMoneyEarned, 1000);
});
function updateMoneyEarned() {
moneyEarned += secondSalary;
$("#moneyEarned").html(accounting.formatMoney(moneyEarned));
}
});
I want to put the Javascript code into a separate "script.js" file. I made the file, correctly linked to it in the HTML, I included jQuery and the accounting.js and everything, and it's not working, not updating.
I tried putting the code straight into the HTML and it's working if I do that, so clearly the problem is the way I make the script.js. I just copied/pasted that code, is there more to it? What am I doing wrong?
Remember that you have to do things in order. You can't include this file if you didn't include jQuery before.
<html>
<head>
<title>...</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="path/to/your/script.js"></script>
</head>
<body>
<!-- your content -->
</body>
</html>
After you have the correct order, you will be able to execute your scripts. If your $(function(){ ... }); call doesn't work, try using it this way:
$(document).ready(function() {
// Your code here
});
and see if it works.
In your jsfiddle i can see that you were including accounting.js in css section:
<script src="//cdn.jsdelivr.net/accounting.js/0.3.2/accounting.min.js"></script>
I moved it to the html section and it started working.
http://jsfiddle.net/GvAzM/1/
Is there a way of calling a function that exists in a HTML file from a .js file?
Something like this:
HTML file:
<script type="text/javascript" src="javaFile.js"></script>
<script type="text/javascript">
function doSomething() {
};
</script>
javaFile.JS file:
htmlFile.doSomething();
? Thanks :)
You can directly call doSomething();