详解js获取当前页面url信息

作者:admin   时间:2020-07-09   访问量:104

  网站的搜索页如何利用JS写出动态的tile呢?首要的条件就是要获取当前页面的url的信息。下面大树就用以本站为例,讲一下JS获取当前页面url的信息,然后改写title、keywords和description的方法:

  var url = window.location.search;

  var arrMeta = document.getElementsByTagName("meta");

  var kw = decodeURI(url.split('=')[1]);

  document.title = '"' + kw + '"' + '搜索结果页' + '-大树SEO';

  for(var i=0;i

  if(arrMeta[i].name == 'keywords'){

  arrMeta[i].content = kw;

  }

  if(arrMeta[i].name == 'description'){

  arrMeta[i].content = '当前页面是'+'"' + kw + '"' +'的搜索结果页,如果您没有找到想要的内容,欢迎留言。';

  }

  }

  JS获取url的方法详解:

  url = window.location.href; /* 获取完整URL */

  alert(url); /* http://127.0.0.1:8020/Test/index.html#test?name=test */

  url = window.location.pathname; /* 获取文件路径(文件地址) */

  alert(url); /* /Test/index.html */

  url = window.location.protocol; /* 获取协议 */

  alert(url); /* http */

  url = window.location.host; /* 获取主机地址和端口号 */

  alert(url); /* http://127.0.0.1:8020/ */

  url = window.location.hostname; /* 获取主机地址 */

  alert(url); /* http://127.0.0.1/ */

  url = window.location.port; /* 获取端口号 */

  alert(url); /* 8020 */

  url = window.location.hash; /* 获取锚点(“#”后面的分段) */

  alert(url); /* #test?name=test */

  url = window.location.search; /* 获取属性(“?”后面的分段) */

  alert(url);

  /* 如果需要URL中的某一部分,可以自己进行处理 */

  url = window.location.pathname;

  url = url.substring(url.lastIndexOf('/') + 1, url.length);

  alert(url); /* /index.html */

  /*

  * 如果页面使用了框架(frameset)

  * 要获取到指定页面的URL

  * 只要把window换成指定的页面即可

  */

  /* 'frame'为指定页面的class名 */

  var url = window.parent.frames['frame'].location.href;

  /* 获取当前地址栏中显示的URL */

  var url = window.parent.location.href;

  /* window parent 可互换 */

  var url = parent.window.location.href;


【地址】:http://www.inseo.cn/seo/33.html转载请注明出处


相关文章

Copyright © 2018-2030 大树SEO All Rights Reserved.