In all dynamic and interactive web site, the window.location.href is used frequently, so it leads to the rule 1:get all the frequent variables the shorter name like

const l=window.location

We make the different url to match different page, so the first step is to analyze the url whatever it is the first time onload or not. Anything before we analyst and use it should be got. To get the url

window.location.href

One of the most wonderful and great place of the JavaScript is that the attribute of the object can aslo be the object. For example, the window is a object, and the location is also a object. Very alike to the real world like we are the object has such as age, weight, height property and so on and the clothes of us aslo have the color, size, thickness property, etc.

<div id='page'></div>

I admit it is too fast to the newbie, but this article is mainly facing the secondary front-end developer. For the beginner, I prepare the primary series.
When the url changed, we call the corresponding function and the default function is Index()

window.onload=window.onhashchange=function(){
  const 
  key=l.href.split('#')[1]
  key ? eval(key+'()') : Index()
}

Notice that we should match the url both onload and onhashchange.


许哥
1 声望0 粉丝

« 上一篇
简历所用代码
下一篇 »
kmp