Common JavaScript methods and techniques

Source: Internet
Author: User

This article focuses on common JavaScript methods and techniques, including creating script blocks, hiding script code, and linking external script files, I hope you will have some knowledge about JavaScript methods and techniques through this article.

Common JavaScript methods and techniques

Create a script block

 
 
  1. 1<scriptlanguagescriptlanguage=”JavaScript”> 
  2. 2JavaScriptcodegoeshere  
  3. 3</script> 

Hide script code

 
 
  1. 1<scriptlanguagescriptlanguage=”JavaScript”> 
  2. 2<!--  
  3. 3document.write(“Hello”);  
  4. 4--> 
  5. 5</script> 

Display when the browser does not support

 
 
  1. 1<noscript> 
  2. 2Hellotothenon-JavaScriptbrowser.  
  3. 3</noscript> 

Link external script file

 
 
  1. 1<scriptlanguagescriptlanguage=”JavaScript”src="/”filename.js"”> 
  2.  
  3. </script> 

JavaScript method annotation script

 
 
  1. 1//Thisisacomment  
  2. 2document.write(“Hello”);//Thisisacomment  
  3. 3/*  
  4. 4Allofthis  
  5. 5isacomment  
  6. 6*/ 

Output to Browser

 
 
  1. 1document.write(“<strong>Hello</strong>”); 
  2.  

Define Variables

 
 
  1. 1varmyVariable=“somevalue”;  
  2.  

String Addition

 
 
  1. 1varmyString=“String1”+“String2”;  
  2.  

String search using JavaScript Methods

 
 
  1. 1<scriptlanguagescriptlanguage=”JavaScript”> 
  2. 2varmyVariable=“Hellothere”;  
  3. 3vartherePlace=myVariable.search(“there”);  
  4. 4document.write(therePlace);  
  5. 5</script> 

String replacement

 
 
  1. 1<scriptlanguagescriptlanguage=”JavaScript”> 
  2. 2thisVar.replace(“Monday”,”Friday”);  
  3. 3</script> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.