Jump to content

how to make .js script file


Recommended Posts

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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...