JavaScript string Properties indexof ()

Source: Internet
Author: User

I would like to be a friend of the Web page is not too unfamiliar with this property function-we can find it from a number of JavaScript reference books: Thestring.indexof (Substring,[n]). It looks for a routed substring in the specified string object, and if you specify parameter n, it can start the search forward (from left to right) from the specified position. If a substring is found, this method returns the position of the substring in the string, and returns 1 if no corresponding string is found.

Using this property, we can call it a lot of work, as long as you command it properly.

I. Judging OS

In general, judging your visitors ' operating system will make them think you're done, but not always. Although the HTTP protocol is cross-platform, the final display of HTML pages you write is often related to the operating system used by the client. For example, the MSOs series (Ms-dos,windows 9x/nt, etc.) for the text "another line" is defined as "carriage return add line", but the Mac as long as a "return" is enough, and Unix/linux only use "line break". So you can export some of the things you want to branch in a certain page, which may result in a "compatibility" problem ... Never mind, look at the following example.

First step. Insert the following script at a location in the page:

<script language="JavaScript"><!--
var newline=''; //定义"另起一行"的全局变量
var browserVer=navigator.appVersion;
if(browserVer.indexOf('Win')!=-1){ //如果当前浏览器的版本是Windows版的
newline='
';}
else{
if(browserVer.indexOf('Mac')!=-1){ //如果当前浏览器的版本是Mac版的
newline='
';}
else{ //那因该就是Linux版的了
newline='
';}
}
-->
</script>

Second step. Use it in the page body (<body>......</body>):

<script language="JavaScript"><!--
var win=self.window;
win.alert('第一行'+newline+'第二行'+newline+'第三行'); //可以将newline作为一个变量在任意位置引用
-->
</script>

Two. Judge the browser

The 1998 browser war seems to have vanished, but the resulting browser compatibility problem has been a huge pain to the vast number of web writers. There are a number of solutions to this problem, which are beyond the scope of this article, but I think the most thorough and effective approach is probably to write a page version for each major browser, which is almost perfect. Try the following code, which will help you bring your visitors to the page that corresponds to the client browser:

<!-- 把这段代码放在页面的前部,最好放在<script language="JavaScript"><!--
var browser=navigator.appName;
var version=navigator.appVersion;
var ver_number=parseFloat(version.substring(0,version.indexOf('(')));
//如果客户端浏览器是ie并且版本高于4.0
if(browser.indexOf('Microsoft Internet Explorer')!=-1&&ver_number>=4.0) top.location='/ie4/index.html' //假设你为4.0及更高版本的ie准备的页面是/ie4/index.html
//如果客户端浏览器是Netscape并且版本高于4.0
else if(browser.indexOf('Netscape')!=-1&&ver_number>=4.0) top.location='/nc4/index.html' //假设你为Netscape 4.X准备的页面是/nc4/index.html
else top.location='/normal/index.html' //假设你为其他浏览器及低版本的NC、IE准备的页面是/normal/index.html
-->
</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.