Html page not executing the java script - javascript

This is a simple hello world in java script which is ref. in html via separate js file. View Page Src of html shows this:
<html>
<head>
<script type="text/javascript" src="HelloWorld.js"></script>
</head>
</html>
HelloWorld.js :
<html>
<header>
<script language="javascript" type="text/javascript">
document.write("Hello World!");
</script>
</header>
</html>
But Hello World not displayed in browser when I double click the html file.

remove html, header, script tag in your js file
document.write("Hello World!");

you js file contains HTML
<html>
<header>
<script language="javascript" type="text/javascript">
document.write("Hello World!");
</script>
</header>
</html>
Instead it should contain only js code like
document.write("Hello World!");

Related

Calling a java script function inside HTML

I am trying to print something inside my html from a java script. I did this but it don't work:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>I am trying to print <script>go();</script></p>
<script>
function go() {
document.write("Hello World!");
}
</script>
</body>
</html>
You're function hasn't been defined yet in the example you are posting, so call to go effectively does nothing, change the ordering of your script tag
<!DOCTYPE html>
<html>
<head>
<script>
function go() {
document.write("Hello World!");
}
</script>
</head>
<body>
<p>I am trying to print <script>go();</script></p>
</body>
</html>

How to execute a simple jQuery Program?

i have created a simple jQuery program.i am new to jQuery technology..please provide me where am i wrong?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"
>
<html lang="en">
<head>
<title><!-- Insert your title here --></title>
<script type="text/javascript" src="jquery-1.4.2.min.js">
$(document).ready(function() {
alert("jQuery tutorial for beginners Example");
});
</script>
</head>
<body>
<p> i m here hid me</p>
<!-- Insert your content here -->
</body>
</html>
The script you've written isn't supposed to go inside the jquery script tag. It needs to go in its own one.
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
alert("jQuery tutorial for beginners Example");
});
</script>
The first one is to load jquery itself - it's like saying "instead of me writing some javascript here, load it from this file instead", the second one is for your own code since you've put code between the script tags.
A script element can have one script. That script can go between the start tag and the end tag or it can go in another file and be referenced with a src attribute.
If there is a src attribute, then the content of the element will be ignored.
If you want two scripts, then you need two script elements.
<script src="jquery.js"></script>
<script>
// Your code
</script>
Example: Jsdiffle
This is a example with last library of Jquery.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"
>
<html lang="en">
<head>
<title><!-- Insert your title here --></title>
<script src="http://code.jquery.com/jquery-2.0.0.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
alert("jQuery tutorial for beginners Example");
});
</script>
</head>
<body>
<p> i m here hid me</p>
<!-- Insert your content here -->
</body>
</html>

How do I call individual methods from a Javascript file

In the javascript assets file I have a jstester.js file like this:
function hehe()
{
alert("wedsdsd");
}
document.write("fdygsdfysdgf");
Then in the public index.html file I have this:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script src="/assets/jstester.js">
hehe();
</script>
</body>
</html>
So I thought that is how I can call a method from my Javascript file but looks like it is not working, no message box shows up... So what is the correct way of doing this?
If a <script> has a src then the text content of the element will be ignored.
so you can't do:
<script type="text/javascript" src="assets/jstester.js">
hehe();
</script>
but:
<script type="text/javascript" src="assets/jstester.js"></script>
<script>hehe();</script>
This is what you looking for:
<body>
<script src="/assets/jstester.js"></script>
<script type="text/javascript">hehe();</script>
</body>

Getting data from another file

I want to get the data which is in <script></script> tags on hello.html to index.html. How can i do that?
index.html:
<head>
<script>
// some codes
</script>
</head>
<body>
<div>
<!-- result will be come here -->
</div>
</body>
</html>
hello.html:
<html>
<head>
</head>
<body>
<script>
document.writeln("hello world");
</script>
</body>
</html>
edit: i want the "document.writeln("hello world");" part, it must be string.
save the script separately as myScript.js and then you can use it in both html files with the script include tag! <script src="myScript.js"></script>
Move the script into a new file, such as script.js, then add this to both files:
<script src="script.js"></script>

How to embed a jquery library in javascript?

I have a jQuery library code in jquery.xyz.js .
I have an html file which uses the function defined in jquery.xyz.js in this manner .
<html>
<body>
<script type="text/javascript">
document.write("This is my first JavaScript!");
$(function(){ $("ul#xy01").xyz(); });
</script>
</body>
</html>
But the jQuery is not running, which I am assuming because I haven't loaded the jQuery properly onto the html page. So how do I do it?
<script type="text/javascript" src="jquery.js"></script>
See how jQuery works in the manual for the basics, and the download page to fetch the library (or to find out addresses for direct linking on a Content Delivery Network).
You just need to include the script files before using functions defined in them ($ is just a function), for example:
<html>
<body>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.xyz.js"></script>
<script type="text/javascript">
$(function(){ $("ul#xy01").xyz(); });
</script>
</body>
</html>
Be sure to include jQuery before plugins that rely on it, or you'll get some errors first thing.
<script>
var script = document.createElement('script');
script.onload = function () {
//do stuff with the script
};
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/xx.xx/jquery.min.js';
document.head.appendChild(script);
</script>
<script type="text/javascript" src="PATH TO YOUR JS FILE"></script>
Stick this above your jQuery code in the <head></head>
<script src="/js/jquery.js" type="text/javascript"></script>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js">
</script>
</head>
<body>
<script type="text/javascript">
document.write("This is my first JavaScript!");
$(function(){ $("ul#xy01").xyz(); });
</script>
</body>
</html>

Categories