Home >Web Front-end >JS Tutorial >JavaScript Tips: Let the web page automatically put on a coat

JavaScript Tips: Let the web page automatically put on a coat

高洛峰
高洛峰Original
2016-11-25 11:20:40870browse

When designing web pages, many web pages require the same border pattern and navigation bar. FrontPage provides us with shared borders to facilitate design. However, this is not very convenient. After all, the same content is added to each web page, which invisibly changes the web page. Increased, of course, for dynamic web pages, this is not a problem, but what if it is static?

How to overcome this shortcoming? This can be achieved through the frame. However, the frame has caused another trouble, that is, if you directly open the internal page of the frame and take off the gorgeous coat, will it look very monotonous? Here we find A good way is to let the webpage that takes off its coat automatically put on its coat, have you thought of it? If you are not interested in this topic, you can walk away and we will discuss it in detail below.

In frame web pages, the src parameter is usually used to specify the web page address within the frame. What we have to do is, when opening this address directly, let it automatically detect and then put on the coat. Of course, we must first add detection in the web page The code is as follows:


<script><br>if(top.location==self.location)<br>{<br> top.location="index.htm?"+self.location;<br>}<br></script>


It’s that simple. Note that index.htm is the coat webpage address. The next thing to do is how to make the coat webpage automatically add this part of the content. We need to parse the webpage address in the coat webpage, find the parameters, and then add the frame Just point the src parameter to this parameter. The code is as follows:


<script><br>document.write(<iframe id="mid" name="mid" width="100%" height="100%" frameborder= "0" scrolling="auto")<br/>var n=self.location.href.indexOf("?")//Check whether the parameter is included<br/>if(n>0)//The parameter exists<br>{<br>//Point to the parameter<br> document.write(" src="+self.location.href.substr(n+1))<br>}<br>document.write(></iframe>)<br></script>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn