0

Is it possible to extract the value of a variable used in java script in a web page. I need to get the url stored in javascript function in a web page. In web page the function is called using <a href,

when we click on that hyperlink , java script function is called and the url is returned.
Below is the code,

<a class="dld" OnClick="download_file();" href="javascript:">&nbsp;</a>

The script function is

function download_file() {
    var summaryFlag     = 0;
        if(document.getElementById("frmincludefile").checked){
    summaryFlag=1; }
    url = '/filedatabase/file_process_request.html?mru=53616c7465645f5f3f24b8f4a86301eb202e67fbb679df4356e24fee9d7a17a099010eb5acf5985c&type=doc';
    url += '&sum='+summaryFlag+'&logo=0&xcode=xtritiuminx&serve_txt=0&folderid=';
    document.location   = url;
}

How can i get value of url variable.

4
  • What do you mean "in a web page"? In code behind? Commented Jul 24, 2012 at 6:44
  • No, I'm extracting the content from a web page. I need to know how to extract the value of variable used in javascript. Commented Jul 24, 2012 at 6:47
  • Sounds like he is scraping a page he doesn't control. Commented Jul 24, 2012 at 7:41
  • Yes, Bert Evans. I'm scraping the web page. Commented Jul 24, 2012 at 8:43

3 Answers 3

1

I was not able to the result of java script function but now i'm invoking the javascript using webBrowser1.Document.InvokeScript("download_file"); By invoking the java script i'm able to download the file.

Sign up to request clarification or add additional context in comments.

Comments

0

Set the URL var value in any hidden field

for e.g.

document.getElementById("hidden_field_name").value = url; 

1 Comment

I can't edit the function , since i'm extracting data from a web page. The url contains the path to download a file, how to extract that variable content.
0

You can do this using variable in the upper scope. In the example bellow I'm using global variable (bad practice) to save the localUrl value from the redirect function:

var url = '';

function redirect() {
  var googleRadio = document.getElementById('google');
  if (googleRadio.checked) {
    localUrl = 'http://google.com';
    url = localUrl;
  }
}

2 Comments

Thank you for your reply, Minko Gechev. But i'm scraping the page, i can't write any functions in the page. How to get the value once the page load is completed.
If you are calling the download_file on mouse click you can get the value only on user interaction. You also can simulate it with firing the event (trigger of jQuery) but I don't think that the code will be very "beautiful" and readable in that case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.