WebControl - Modify the javascripts location.href variable, without redirect

Basically, I have a custom browser made with Visual basic's Web Control object.

I have added one to the stage, and if I load a temporary file (Say c:\temp.html) into it, and call a javascript that says


It will bring back file:\\c:\temp.html

I would like to modify this to be my companies main website url. I can modify the file before hand, but I am unsure how to change javascript's location.href variable without it trying to refresh the page, but so every time javascript calls it, or location.path etc, it will use my custom values.

Any help is appreciated
Who is Participating?

Improve company productivity with a Business Account.Sign Up

GHCS_MarkConnect With a Mentor Commented:
You can not do that without completely rewriting the runtimes.  The Location.href javascript function takes the current windows address location.  Therefore, if you modify this value it will attempt to navigate there automatically.  Your only way around this is to set up a website on a local instance of IIS using the header value property and modify your C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS. file to point at your local machine for that domain: www.mywebsite.com
zzzzzoocConnect With a Mentor Commented:
You could just replace any references to 'location.href' with your custom URL.

Private Sub Command1_Click()
    Const html_original As String = _
        "<html><head><script language=javascript>alert(location.href);" & _
    Dim html_new As String
    html_new = Replace$(html_original, "location.href", "'http://www.site.com'")
    WebBrowser1.Navigate "about:blank"
    WebBrowser1.Document.write html_new
End Sub

Open in new window

ocnod1234Author Commented:
Thanks for your comments

GHCS - I've thought about doing this with a tomcat server, but when looking into it I realised all traffic to that domain would be transferred to the tomcat server, including images and external javascripts. The problem being that I would have to build in a pretty extensive tomcat apache REWRITE_URI and possible a php system for what I wanted to be a fairly simple project :)

zzzzzoc - I have tried this method and the only issue becomes when either the javascript is included through a <script src=""> tag, or if the location.href is locationed inside of a <a href='#' onClick='window.location.href = location.href +"?id=video1'">

In either of the above 2 cases, the replace would fail, and since I don't have control over the websites it's failing on (Mail.yahoo.com) , not to mention the sites I don't know it will fail on I'm not sure that I could account for that and also account for only javascript files. I'm decent with VisualBasic, but no where near advanced  :)

Aside from writing a download routine to download the external javascripts by parsing the <script tag and pulling the source url, and then replacing it in the original with an actual javascript tag and then run the routines on it, while making sure it keeps in <script tags only, aswell as onClick and other behavior tags, and doesn't get replaced on say a website talking about the subject, I think it might work, but I can't begin to imagine how I would start a project to parse that complex of a scenario and replace the location.href for each one without interfering with the workings of the script. Is it surrounded in a " " , or is it used as a string in a javascript, or is it inside of the text of the website and not being used as a script?

If theres something I'm missing, and believe me, there probably is :) Please let me know, I've been working on this and will post any solutions I might find that seem more robust and managable.

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

>>and since I don't have control over the websites it's failing on
I thought you were working with local files?

I don't think you'll be able to easily override the location property unless you override the actual DOM itself with your own classes, if possible. Even then, while working with remote domains, security (and other) issues may arise when the location doesn't genuinely match the content.

If you can pre-download the pages, modify them appropriately then display them locally, it might be the easiest (yet lengthy) solution. From what I've worked with, the <head> element (which includes the scripts) is somewhat static and can't be modified without destroying the original document (ie: what you've already been doing). I would figure it'd be more dynamic and maybe I'm wrong, but my tests are a no-go.

If you do attempt my approach, in regards to .js files, you can pre-download them and append them to the <head> element. It should work, and if not (due to reasons mentioned above), IE would probably accept <scripts> in the <body>. element.
What exactly is the reason for the custom browser, and what are you trying to achieve?  Your comments here seem to suggest there is a bigger picture that would help in answering your question.
ocnod1234Author Commented:
Basically I would like a way to create an ad-free browser. Since a lot of ad's are generated by big websites with a single <script src=""> include method, then I figured, well, easy, I'll just replace the url with something to my server that's blank.

Turned out that wasn't so easy, and once I replaced the outerHTML it didn't update. If I replaced innerHTML, sure, it worked, but ran the javascript from the ads first, kinda defeating the purpose. I tried the navigate to about:blank then write the content in, and that kind of worked, but I ran into issues with location.href pointing to the temporary file. It seemed the most promising of my tests though, as it's the only one that let me modify the content prior to it running the onLoad javascripts.

Hope this helps explain it a bit better.
Actually, in response to my previous response...

If you really wanted to do this, you would add some javascript code to change it to the link you wanted.

Let's say you had 'mywebsite.com' as your website.

Then you would add javascript within the page like:
<script language="JavaScript"><!--
parent.document.links[0].href = 'mywebsite.com';

ocnod1234Author Commented:
Hi Cool,
Thanks for your comment and time.

The only problem with the solution proposed above is that it doesn't replace the javascript driven links that use the location.href object to populate the links dynamically (As found in mail.yahoo.com)

If you can use the temp file method, and navigation and login to and use mail.yahoo.com successfully, then this will definitely work for me,  and I will give it a shot shortly, but from my tests, yahoo uses the onLoad function to redirect using location.href as a base url , and that redirection that happens at on load is the issue I'm having trouble fixing.

Hope this helps better explain the situation, my biggest issue is that mail.yahoo.com doesn't work, and the reasoning is the location.href, but it's not an actual html link, so the javascript to replace the links[0] might not work because it's a dynamically driven variable loaded at the onLoad statement.

I will attempt adding this directly after the HEAD tag, and then work from there. If it does indeed change the location.href object's reference link, then I give you props :)

The only other solution I've seen recently is to move into using Firefox's open source system as it allows full access to everything, unlike IE's web browser control where it's more like a wrapper object.

Zack M.
PS, please close the previous question that I answered as per the EE rules.

ocnod1234Author Commented:
Hi Cool,
the previous question still has yet to receive a working solution. The fact that it works in some cases, but not all I honestly do not feel as a solution, since I am trading one issue for another.

As I have thoroughly explained my situation, including the one you mentioned as the solution, prior to your posting, I would clearly see this as proof that I had tried your method, and did not find it a viable solution, which is why I included the Code Set 3 in my original post to show that I have already attempted this method but ran into issues, so therefore it's not a solution, but a work-around with more issues that arise.

As I mentioned in my previous post on there, Yours is the closest to a solution, but it is with severe complications, and unless these can be overcome, it's not a true solution which I hope you understand. I also stated that since this seems to be un-solvable, at least through microsofts web control, that if I did not receive a valid solution any time soon, then I would mark yours as the accepted solution to close the question, but my issue still remains, therefore I do not wish to close it. I am paying for a membership in the hopes that I would find a simple way to replace outterHTML content on a site without reprocutions, which I have not found yet.

Zack M.

P.S. I really do appreciate the time you took in your answers and replies and time looking into this, so please don't take it the wrong way, I am in no way trying to cheap any body out on points, I simply want to find a working solution to my issue that doesn't simply bring up different issues in it's place that soley arise from implementing the first solution.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.