Jump to content

how to make .js script file


nedoo2002us

Recommended Posts

Dear all members i want to use external java script through .js file please any one give me some example that how to make .js script file and how to connect it with html document and also tell me how to call java script function through the .js file please help

Link to comment
Share on other sites

This will work, if I don't have got anything wrong.javascript.js

function display_alert(){	alert("Hello!");}

html.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>	<head>		<title>Show Javascript alert</title>		<!-- Link to the JS-file -->		<script type="text/javascript" src="javascript.js"></script>	</head>	<body onload="display_alert();">	</body></html>

Link to comment
Share on other sites

Yeah you can create any javascript(.js) and then call it from youre html very simple.You just open youre code editor and write youre javascript code without the <script type="text/javascript">......</script>.You just write the nested code and then save it as (.js). Then go to youre html code and call it with this way:<script src="abc.js"></script>.

Link to comment
Share on other sites

Here's a best-practice version of Peppe's example:

function display_alert(){	alert("Hello!");}window.onload = display_alert;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>	<head>		<title>Show Javascript alert</title>		<!-- Link to the JS-file -->		<script type="text/javascript" src="javascript.js"></script>	</head>	<body>	</body></html>

There are a few things you can do to that depending on your preferences. Since it's an external JS file, you can remove the type="text/javascript" (because the server tells its type). And if you move the script tag to the end of the body, you can remove the function and assignment to window.onload, and just do what needs doing when the body is fully loaded. (Although you don't need to wait for the body to load before alert'ing anything.)

Link to comment
Share on other sites

Here's a best-practice version of Peppe's example:
function display_alert(){	alert("Hello!");}window.onload = display_alert;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>	<head>		<title>Show Javascript alert</title>		<!-- Link to the JS-file -->		<script type="text/javascript" src="javascript.js"></script>	</head>	<body>	</body></html>

There are a few things you can do to that depending on your preferences. Since it's an external JS file, you can remove the type="text/javascript" (because the server tells its type). And if you move the script tag to the end of the body, you can remove the function and assignment to window.onload, and just do what needs doing when the body is fully loaded. (Although you don't need to wait for the body to load before alert'ing anything.)

thanks buddy's
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...