为什么我不能在IE7中使用相对URL? [英] Why can't I use relative URLs with IE7?

查看:83
本文介绍了为什么我不能在IE7中使用相对URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Google上搜索,似乎无法找到这个问题的答案。我的问题如下:

对于我的jquery,我需要我的链接是相对的而不是绝对的。我的PHP设置为返回相关的URL,并且一切正常,直到我在IE7中测试它。出于某种原因,IE7不断将我的相关URL改为垃圾,这打破了我的js脚本。这是正常的吗?有没有办法解决它?



例如:

IE8,Chrome,Firefox,Safari等 -

 < a href ='/ page'onclick ='click_handler(this); return false;'> clicky< / A> 

IE7 -

 < a href ='http://www.myurl.com/page'onclick ='click_handler(this); return false;'> clicky< / a> 


解决方案

我在init处抓住baseUrl, :

  var baseUrl = window.location.href.substring(0,window.location.href.lastIndexOf(/) + 1); 

...然后在我的URL处理程序中,去掉baseUrl:

  var url = $(this).attr(href)。replace(baseUrl,); 

您也可以使用 .support来检查href是否标准化 )

  $。support.hrefNormalized 

(如果浏览器在获取href值时未做任何修改,则返回 true ,所以它当前为false IE。)


I've been Googling for a while and can't seem to find an answer to this question. My problem is as follows:

For my jquery, I need my links to be relative rather than absolute. My PHP is set to return relative urls and everything is working fine, until I test it in IE7. For some reason, IE7 keeps changing my relative urls to abosulute, which breaks my js script. Is this normal? Is there a way to get around it?

For example:

IE8, Chrome, Firefox, Safari etc -

<a href='/page' onclick='click_handler(this);return false;'>clicky</a>

IE7 -

<a href='http://www.myurl.com/page' onclick='click_handler(this);return false;'>clicky</a>

解决方案

What I do is grab the baseUrl at init, like:

var baseUrl = window.location.href.substring(0, window.location.href.lastIndexOf("/") + 1);

... and then in my URL handler, strip the baseUrl:

var url = $(this).attr("href").replace(baseUrl, "");

Also you can check if the href is "normalized" using .support():

$.support.hrefNormalized

(returns true if the browser makes no modifications when grabbing an href value, so it's currently false in IE.)

这篇关于为什么我不能在IE7中使用相对URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆