Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2008-06-25
Medium Priority
Last Modified: 2008-07-07
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
Question by:ocnod1234
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1

Accepted Solution

GHCS_Mark earned 1000 total points
ID: 21870804
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:
LVL 17

Assisted Solution

zzzzzooc earned 1000 total points
ID: 21870932
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", "''")
    WebBrowser1.Navigate "about:blank"
    WebBrowser1.Document.write html_new
End Sub

Open in new window


Author Comment

ID: 21885940
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 ( , 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.

Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

LVL 17

Expert Comment

ID: 21887864
>>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.

Expert Comment

ID: 21894677
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.

Author Comment

ID: 21894765
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.
LVL 10

Expert Comment

ID: 21895276
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 '' as your website.

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


Author Comment

ID: 21895848
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

If you can use the temp file method, and navigation and login to and use 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 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.
LVL 10

Expert Comment

ID: 21902533
PS, please close the previous question that I answered as per the EE rules.


Author Comment

ID: 21902729
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.

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question