How to call javascript file in html - javascript

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";
}

Related

Access a Function From Another File

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.

Running JavaScript specific function from html

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>

how Javascript call to a function of a external file?

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);
}

Extracting JavaScript into separate file (from HTML). Duplicate call to function needed

See the source of http://marakana.com/s/post/1096/samples/try6.htm
It defines a function and calls it on load of document. (Which is the final step of this tutorial)
I tried to put it into a seperate JS file.
Runs correctly only if I call onload both in JS and in HTML.
But not only body onload or only from JS. I guess I am doing something wrong.
So, following works:
<head>
<script src="Scripts/makeWYSIWYG.js" type="text/javascript"/>
<script type="text/javascript">
window.onload = function () {
makeWYSIWYG(document.getElementById('editor'));
};
</script>
</head>
<body onload="makeWYSIWYG(document.getElementById('editor'));">
Why do I need to call the function twice?
I only have the function definition in "Scripts/makeWYSIWYG.js"
function makeWYSIWYG(editor) {
...
return editor;
};
Thanks,
There are no reason to call the function twice. The is enough.
With the first window.onload you could be changing a former function callback assignment (i.e. in a imported script).
The problem was actually the closing tag, "/>", here:
<script src="Scripts/makeWYSIWYG.js" type="text/javascript"/>
I should have written:
<script src="Scripts/makeWYSIWYG.js" type="text/javascript"> </script>
I guess the second script was helping the tag to be closed and making it run...
More info here: Why don't self-closing script tags work?

javascript file talk to script in html file

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();

Categories

Resources