且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Request.UrlReferrer在IE8及更低版本中为null

更新时间:2023-12-04 13:48:42

As我告诉我很久以后我点击我的解决方案我只是用代码详细说明了这些东西。



当用document.location.href或window打开一个新窗口时。在IE8或更低版本中打开,Request.UrlReferrer为null,不在IE9或Firefox或Chrome中。



解决方案是



你曾经使用过像

document.location.href =/ Roles / Index;



将以下函数复制并粘贴到js文件中并调用该函数。



RedirectURL(/ Roles / Index);



函数RedirectURL(url){

var a = document.createElement(a);

if(a.click){

// HTML5浏览器和IE支持上的click(),早期FF没有。

a.setAttribute(href,url);

a.style .display =none;

document.body.appendChild(a);

a.click();

} else {

//然而,早期的FF可以使用这种通常的方法

//其中IE无法使用安全链接。

window.location.href = url;

}

}



希望以上解决方案能解决问题。

I have a task given by Manager that do some research that no body can tampering our site, if someone try to tamper the URL just show a message or page crash.

I tried many ways but finally I found HttpContext.Current.Request.UrlReferrer. This property is readonly and browser dependent. Whenever somebody is trying to change directly in the url then property is going to null and we can write some code accordingly.

This is successfully run on all the browser except the IE 8 or below what I found.
I tried to overcome this issue more than a week but no resolution.

Finally I found one solution in my mind which going to post in Answer.

If anybody has any better solution then please share it in my code project blog.

Thank you.

As I told after long RND I click on solution on my mind I just elaborate the things with code.

When open a new window with document.location.href or window.open then in IE8 or lower, Request.UrlReferrer is null which is not in IE9 or Firefox or Chrome.

The solution is

Where ever you used like
document.location.href = "/Roles/Index";

Just copy and paste the below function in js file and call that function.

RedirectURL("/Roles/Index");

function RedirectURL(url) {
var a = document.createElement("a");
if (a.click) {
// HTML5 browsers and IE support click() on , early FF does not.
a.setAttribute("href", url);
a.style.display = "none";
document.body.appendChild(a);
a.click();
} else {
// Early FF can, however, use this usual method
// where IE cannot with secure links.
window.location.href = url;
}
}

Hopefully the above solution will resolve the issue.