Dynamically embedding javascript in a Windows Phone 8 app

I am just trying to add dynamic javascript to the content of my web browser. My scenario: I want to display an ad that maps to a zone ID 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 the zoneid will dynamically change with 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 First I tried to go 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 has a local HTML page, loading it in the webbrowser load event and then injecting the script into it -

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

      

Both methods work in the second case, when I receive an ad, this ad is clickable as I am getting the entire script associated with that ad (I checked it with the WebBrowser SaveToString () method.)

But the problem is, in the first case, the ad I am getting is not clickable as the script associated with it does not exist. (In HTML page content) (Checked this also with SaveToString ();)

I tried a lot but couldn't find any clue. What should be my approach to find out why this ad is not clickable and why I am not getting a script with it.

I can also post HTML content in both cases if suggested. Thanks in advance, please help me solve this as I am stuck at this stage and need clarification on things.

+3


source to share


2 answers


You can add scripts to the head using JS and they will run as they should, fooobar.com/questions/179405 / ... showing how to do it with working examples and it uses simple javascript.



Hope it helps.

+2


source


In the first case, you enter the complete html into a web browser control that has the javascript source in it, but you never assign that "dynamicTitle". What do you do in the second case using the javascript eval method, and the string in the javascript_tag variable is assigned to the innerHTML "dynamicTitle".

You need to do the same in the first approach, but there is no way to access the html document directly in the Windows phone other than as a string. Below is the code:

add link 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 that, you need to upload the edited html document to the webBrowser again.

+2


source







All Articles