Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing location URL works in IE6 but not IE7

Status
Not open for further replies.

MetroidHacker

Programmer
Feb 21, 2008
4
US
I'm having trouble with some code that behaves differently in Internet Explorer 6 and 7.

Code:
<html>
<body>
<p>Test</p>
<script language=javascript>
    window.location.replace("javascript:document.write(\"<p>Hello, world!</p>\")");
    document.write(window.location);
</script>
</body>
</html>

In IE6, this replaces the document with one line, "Hello, world!", but in IE7, the "Hello, world!" is rolled into the rest of the page and comes out between "Test" and the URL of the original page. The following code has exactly the same effect:

Code:
<html>
<body>
<p>Test</p>
<script language=javascript>
    document.URL="javascript:document.write(\"<p>Hello, world!</p>\")";
    document.write(document.URL);
</script>
</body>
</html>

and replacing document.URL with window.location.href also has the same effect.

It seems strange to set a variable and then read it back, only to find its value has not been changed. The problem seems to be with the handling of the "javascript:" protocol, because using a URL such as " redirects to the website in both browsers, but running javascript has different behavior in the two browsers. I need to replicate IE6's behavior in IE7. Any ideas, anyone?
 
Thanks for the reply. This is, of course, a simplified example. What I want to happen is the whole page to be replaced when the "javascript:" URL is set. When the URL is set, I expect the page rendering to bail out and redirect to the specified URL.

The real reason I want to know how to do this is that I have an ActiveX control that similarly runs some JavaScript by setting the URL. The JavaScript code runs in IE6 but is never run in IE7. I tried some simple examples until I discovered the change in behavior I described above. I thought it must be related to my real problem.
 
I'm sorry, I don't know what you mean. I've shown you the code that doesn't work in IE7. IE7 is not replacing the page when I set the URL. For reference, here is the C++ code from the ActiveX control that doesn't work:
Code:
IHTMLDocument2 *pDisplayDocument;
//pDisplayDocument is initialized from current context
_bstr_t differentUrl("javascript:document.write(\"Hello World!\");");
pDisplayDocument->put_URL(differentUrl);
In IE6, this replaces the ActiveX control with the text "Hello, World!". In IE7, nothing happens at all.

I also tried
Code:
_bstr_t differentUrl("javascript:debugger;document.write(\"Hello World!\");");
IE6 launched the script debugger; IE7 did nothing. On the other hand, if I use
Code:
_bstr_t differentUrl("[URL unfurl="true"]http://www.tek-tips.com");[/URL]
both browsers replace the ActiveX control with the referenced web site. Basically the code in the javascript: URL does not get run in IE7, and I need it to run.
 
Has anyone see this problem before? Can anyone even agree with me that this is a problem? If more clarification is needed, please ask.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top