3

I am just trying to add some dynamic javascript to the content of my web browser . My scenario is ,I want to display an Ad which is mapped to a zoneID that will be entered by the user from the phone screen. To be more precise I just want to add the following javascript

    <script src="http://rq.vserv.mobi/delivery/js.php?zoneid=d03e63db&amp;vr=S-JS-1.0.0"></script>&nbsp;</div>

where zoneid will dynamically vary upon the user input.

I searched a lot for this as I have never worked with JS before ,and I got this article -

http://matthiasshapiro.com/2012/11/26/windows-phone-html-5-app-basics/

There are two ways I tried to implement this First one is I tried to navigate to the hardcoded HTML content including script-

     webBrowser.NavigateToString(string.Format("<html><head><meta name=\"viewport\" content=\"width=device-width,height=device-height,user-scalable=no\"><script type=\"text/javascript\" src=\"http://rq.vserv.mobi/delivery/js.php?zoneid={0}&amp;vr=S-JS-1.0.0\"></script></head><body></body></html>",p_zoneID));

The Second one is having a local HTML page ,Loading it on the webbrowser's Loaded event,and then inject a script into it -

     webBrowser.InvokeScript("eval",
                new string[] { "document.getElementById('dynamicTitle').innerHTML = '" + javascript_tag + "';" });

Both the ways are working in the second case when I get the ad,that ad is clickable as I am getting the whole script mapped with that ad .(I checked it by using the webbrowser's SaveToString() method.)

But the problem is that in the first case the ad that I am getting is not clickable as the script related to that is not there .(In the content of HTML page)(Checked this also using the SaveToString();)

I have tried a lot but unable to find any clue. What should be my approach now to know why that ad is not clickable and why I am not getting the script mapped with it .

I can also post the HTML content in both the cases if suggested. Thanks is advance, please help me to go solve this as I am stuck at this point and need clarification of things .

  • 1
    Let me clarify this: what you want is to display an ad when the user enter into a certain zone on your screen. Thus I do not understand why the second case does not work for you? Anyway your HTML would be helpfull. – Ouarzy Aug 20 '14 at 07:53
  • When you go to the javascript url "http://rq.vserv.mobi/delivery/js.php?zoneid=d03e63d&vr=S-JS-1.0.0 in a browser, what html do you receive back? please paste the result as part of your question and I will try and help – FlemGrem Aug 21 '14 at 10:07
  • 1
    Side note: **eval** == **evil** Do not use this tag until you are 100% sure you know what you are doing. – GuyT Aug 22 '14 at 09:44

2 Answers2

2

You can append scripts to the head using JS and they will run as they should, here is a great answer on StackOverflow showing how it is done with working examples and it uses plain javascript.

Hope that helps you.

Community
  • 1
  • 1
Liam Kenneth
  • 954
  • 1
  • 11
  • 20
2

In the first case you are injecting the full html in the web browser control which is having javascript source in it but you are never assigning that to the "dynamicTitle". Which you are doing in the second case using eval method of javascript and the string in the variable "javascript_tag" is getting assigned to the innerHTML of the "dynamicTitle".

You need to do the same in the first approach as well but there is no way to access the html document directly in windows phone except as a string. Below is the code:

add reference to HtmlAgilityPack. Follow this link: http://www.tareqateik.com/html-agility-pack%E2%80%93windows-phone-8#.U_eAtPmSySp

//fetch the html as string
string htmlAsString = webBrowser.SaveToString();
//create an html document from the string 
HtmlDocument htmlDocument = new HtmlDocument(); 
htmlDocument.LoadHtml(htmlPage);
htmlDocument.GetElementbyId("dynamicTitle").InnerHtml = javascript_tag;

After this you need to load the edited html document in the webBrowser again.

Nipun Anand
  • 463
  • 2
  • 6