Click here to Skip to main content
15,883,908 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,just want to know is the mandatory to write script tag to execute javascript code ,if it is mandoatory then how to execute javacript code without script tag

example

<script > alert() </script> 
this will be executed

but if i write

//<script >  
alert()
//</script > 
this will not execute

is there anything alternative to script tag to excuted javascript code
Posted
Comments
Wombaticus 26-Jul-14 10:04am    
Why? What are you hoping to achieve?

For inline code, yes, it s mandatory. Javascript calls can be made within event handlers though without it - eg: <input type="button" onclick="alert('hi')" />
Sergey Alexandrovich Kryukov 26-Jul-14 16:28pm    
Why?!
—SA
Sandeep Singh Shekhawat 26-Jul-14 17:36pm    
I have deleted my solution !!

Yes, it is mandatory. You have to write that, otherwise browsers can't understand what exactly you are trying to do.

In simple words, I can say that HTML is a Markup language and that is only thing understood by the browsers. So, everything is represented by tags in a HTML document.

When you try to execute any JavaScript code, you need to wrap it with HTML Script tags. That is the logic.
 
Share this answer
 
Why don't you write the code in a JavaScript file, and load it in HTML file? For example:
In test.js:
alert('test');


In index.htm:
XML
<!DOCTYPE html>
<html>
    <body>
        <script src="test.js"></script>
    </body>
</html>
 
Share this answer
 
v2
If you are doing inline coding, you must include script tag, or else create a js file and write your codes in that, finally just add the reference to that js file in your page.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900