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<scriptlanguagescriptlanguage=”JavaScript”>
- 2JavaScriptcodegoeshere
- 3</script>
Hide script code
- 1<scriptlanguagescriptlanguage=”JavaScript”>
- 2<!--
- 3document.write(“Hello”);
- 4-->
- 5</script>
Display when the browser does not support
- 1<noscript>
- 2Hellotothenon-JavaScriptbrowser.
- 3</noscript>
Link external script file
- 1<scriptlanguagescriptlanguage=”JavaScript”src="/”filename.js"”>
-
- </script>
JavaScript method annotation script
- 1//Thisisacomment
- 2document.write(“Hello”);//Thisisacomment
- 3/*
- 4Allofthis
- 5isacomment
- 6*/
Output to Browser
- 1document.write(“<strong>Hello</strong>”);
-
Define Variables
- 1varmyVariable=“somevalue”;
-
String Addition
- 1varmyString=“String1”+“String2”;
-
String search using JavaScript Methods
- 1<scriptlanguagescriptlanguage=”JavaScript”>
- 2varmyVariable=“Hellothere”;
- 3vartherePlace=myVariable.search(“there”);
- 4document.write(therePlace);
- 5</script>
String replacement
- 1<scriptlanguagescriptlanguage=”JavaScript”>
- 2thisVar.replace(“Monday”,”Friday”);
- 3</script>