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();
Related
I wrote a function in Html and it works well.
But My teacher said we need separate the code out of HTML file. So I need to implement this code in a .js file. Can anyone tell me how to do that? I think to create a function in JS like this but it not working.
Thanks for any help!
<script>
$(document).ready(function() {
$("#down").on('click',function(event) {
$('html,div-b').animate({scrollTop: document.body.scrollHeight
-1100},"slow");
});
});
You have use html file like this
<html>
<body>
<script src="demo.js">
</script>
</body>
</html>
and keep the js file like this
demo.js file
function test(){
<!---your code here-->
}
copy the code inside the <'script> tag and paste in a separate .js file . This is how it works
Put all the js code in a .js file, then put the code below in the html page which will call it, inside the head or the body.
<script src="myScript.js"></script>
Write the code as it is in file and save it with .js extension and link it in html under head tag as follows
<script src="myscripts.js"></script>
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
Abit new to html and javascript.
I got a JS script and an html code.
I want to run a specific function in my JS form my html code. how do i do that?
My JS generalRedirect.js:
var redirect = {
test1: function () {
window.alert("sometext");
},
....
My HTML:
..
<script src="#Url.Content(".../Scripts/generalRedirect.js")" type="text/javascript"></script>
I want to run a specific function in my JS form my html code. how do i
do that?
You can call your function like this:
redirect.test1();
and don't forget to put that inside <script> tags and also be sure to include the correct file.
Do this:
<script src='#Url.Content(".../Scripts/generalRedirect.js")' type="text/javascript></script>
<script>
redirect.test1();
</script>
Try to do something like this:
<script src='#Url.Content(".../Scripts/generalRedirect.js")' type="text/javascript"></script>
<script>
redirect.test1();
</script>
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 innerHtml in JavaScript its working in the same file but not in the separate JavaScript JS file.
my working code is
<script type="text/javascript">
function my()
document.getElementById("abc").innerHTML="hello";
}
</script>
<div id="abc" onmouseover ="my()"> hi hw ru </div>
But if I invoke this method in separate JavaScript file its not working even I am giving the source path of the JavaScript file like
<script type="text/javascript" src="js/framemrq.js">
Missing the function keyword
<script type="text/javascript">
function my(){
// Your code here
}
</script>
please define your my function correctly like this:
function my() {
document.getElementById("abc").innerHTML="hello";
}