I receive some html data from server, and I try to build a webview to show these data.
Then I need to set some labels to navigate to other page of UWP.
In Windows Phone 8 projects, I can add a label like this to navigate to another page and pass parameters.
But UWP doesn't support it anymore, so how to navigate to another page and pass parameters??
Here is a solution.
1.This is the string that webview will navigate to.
"<html>"+
"<body>"+
"<script type='text/javascript'>" +
"function Click()" +
"{window.external.notify('parameter1+" + "parameter2"+"...");}" +
"</script>" +
"<a href='#' onclick='Click();'>" + "NameOfLabel</a>" +
"</body>"+
"</html>";
2.Webview add a scriptnotify event in your xaml.
<WebView ScriptNotify="webView_ScriptNotify"></WebView>
3.Get parameter from javascript
private async void webView_ScriptNotify(object sender, NotifyEventArgs e)
{
//Get the paramter from javascript
string parameter=e.value;
}
4.One more, there is another wat to pass complicate parameters like a class.
But I can't find this artilce, I will add the url later if I find it.
Related
On my Silverlight project I have created an HTM page where I included an Iframe tag.
I want to open this page on a new browser tab. This is what I have trying to implement on a button click event:
HtmlPage.Window.Navigate(new System.Uri("../Forms/Page.htm", System.UriKind.Relative), "_blank");
HtmlPage.Window.Navigate(new System.Uri("../Forms/Page.htm", "_blank");
HtmlPage.Window.Navigate(new System.Uri("/Forms/Page.htm", "_blank");
HtmlPage.Window.Navigate(new System.Uri("Page.htm", "_blank");
So far none of this approaches worked. I keep getting 404 error saying that page was not found.
Is there anyway to work around this issue?
Best regards
Your page must be in the web project.
If page is located at the project's root, this works (anywhere in solution):
HtmlPage.Window.Navigate(new System.Uri("/page.htm", UriKind.Relative), "_blank");
If your page is inside silverlight application project, you can not access it directly by HtmlPage.Window.Navigate, however you can load its content (then for example save it to local folder and finally display it or use WebBrowser as explained here)
var sm = Application.GetResourceStream(new System.Uri("page.htm", UriKind.Relative));
StreamReader reader = new StreamReader(sm.Stream);
string content = reader.ReadToEnd();
browser.NavigateToString(content);
Use following code to open web page in silver light.
HtmlPage.Window.Invoke("ShowBrowserIFrame", url);
ShowBrowserIFrame is JS function, add this function to html page of silver light.
function ShowBrowserIFrame(url) {
BrowserDivContainer.css('display', 'block');
$('<iframe id="' + jobPlanIFrameID + '" src="' + url + '" style="height:100%;width:100%;" />')
.appendTo(BrowserDivIFrameContainer);
slHost.css('width', '0%');
}
Check this post for complete solution.
I've got a css file injected into my asp.net web form application page through a base page. The method I use look like the following:
private void InjectLocalStyleSheet()
{
if (this.Page.Header == null)
return;
Literal cssFile = new Literal()
{
Text =
#"<link rel=""stylesheet"" type=""text/css"" href=""" + Page.ResolveUrl("~/Common/Theme.css") +
#""" />"
};
Page.Header.Controls.Add(cssFile);
}
When I run the page in firefox, that css file gives me a 302 warning. Apparently, firefox view this file as html type while I've specified the type to be "text/css".
snap of request and response headers
I also run the web page in Chrome and get "Failed to load resouce: net::ERR_TOO_MANY_REDIRECTS"
Anyone has an idea about what is going on? Please help. Thank you.
I'm trying to load a webview using HttpRequestMessage in Windows Phone 8.1. The problem is that the Content-Type header is missing in the content headers when checked in Fiddler.
byte[] postData = GetWebviewPostDataBytes();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, prepareUri(url));
var httpContent = new HttpBufferContent(postData.AsBuffer());
httpContent.Headers.Add("Content-Type", GetContentType());
request.Headers.Add("User-Agent", GetUserAgent());
request.Content = httpContent;
webView.NavigateWithHttpRequestMessage(request);
I found some links where this is posed as an internal bug. Can someone tell me a workaround to this?
The only solution I've found for this is to use a form to load your page.
I'm loading a local HTML page which I'm generating with my POST parameters. This page contains an hidden HTML form which I will submit once the page will be loaded.
The web page loading is then:
generate a local HTML page with a form and your parameters in hidden input fields
load this page in your webview
once the page is loaded, request a submit of the form using the webpage postContent() function
the webview will then navigate to the expected webpage with the content-type header set
This sample code is in WinJS but it can easily be transposed in C# since it is the same webview component.
var htmlContent = "<html><head><script type='text/javascript'>function postContent(){document.getElementById('postForm').submit();}</script></head><body><form id='postForm' action='{targetUrl}' method='post'>{inputFields}</form></body></html>";
var inputFields = "";
var iterator = payloadContent.first();
while(iterator.hasCurrent)
{
inputFields += "<input hidden='on' name='{n}' value='{v}'/>".replace("{n}", iterator.current.key).replace("{v}", iterator.current.value);
iterator.moveNext();
}
var htmlContent = htmlContent.replace("{targetUrl}", "YOUR URL HERE").replace("{inputFields}", inputFields);
this._webviewElement.navigateToString(htmlContent);
In the page loaded event you will then have to request the webview to execute the 'postContent()' javascript function to submit the form.
this._webviewElement.invokeScriptAsync("postContent");
Hope this helps.
I am working on a WP8 application, containing the WebBrowser control in which I open a html page, containing javascript. The javascript contains the following function:
function send(data) {
windows.external.notify(data);
var xhr = new XMLHttpRequest();
xhr.open('GET', 'getresponse', false);
xhr.send(null);
var result = xhr.responseText;
if (result) {
return JSON.parse(result);
}
}
Basically this function calls the native C# side of the app, where I run some functions and I need to be able to return some data from the native side to the send function. I wanted to use an XMLHttpRequest for this, where my idea was to "intercept" the request url (in this case 'getresponse') and return the data I want by including it in the response.
Is this please possible on Windows Phone 8 using the WebBrowser control?
Once again, all I need to do is this:
Have a javascript function (in this case called "send") which connects to the native app (using windows.external.notify) and pass data back to this "send" function so that it can return it (and so that other JS function can use it).
Is this please possible? If not using the XMLHttpRequest, maybe using another technique?
Thank you all for your help!
You are looking for InvokeScript.
If you have full control over the page that is displayed inside the WebBrowser (e.g. the server is your's), you can define the JS-function to be called:
webBrowser.InvokeScript("yourJSFunction", "param1", "param2");
If you display a website from a foreign webserver you can inject JS like this (this uses jQuery):
webBrowser.InvokeScript("eval", "window.youInjectedFunction = function() {" +
"window.external.notify('and_notify_back');" +
"}; " +
"window.readyStateCheckInterval = setInterval(function() {" +
"window.external.notify('timer');" +
"if (document.readyState === 'complete') {" +
"clearInterval(window.readyStateCheckInterval);window.yourInjectedFuntion();" +
"}}, 100);" +
"");
I used the timer, as you can not be certain if the InvokeScript is called after the page is completely loaded.
If you can control the source, you should definitely go for option 1.
I have written a selenium script which runs in java :
String pageName = "my test url which invloves an link to html which has javscript excution";
logger.log(Level.INFO,"Page name : " + pageName);
WebDriver driver = new HtmlUnitDriver(true);
logger.log(Level.INFO,"driver instance created " );
String str ="";
logger.log(Level.INFO,"opening the url now.... " );
driver.get(pageName);
logger.log(Level.INFO,"url is now opened :: url = "+driver.getCurrentUrl());
logger.log(Level.INFO,"driver now going to sleep = "+driver.getCurrentUrl());
Thread.sleep(150000);
logger.log(Level.INFO,"Wake up from sleep now....");
logger.log(Level.INFO,"URL ::"+driver.getCurrentUrl());
logger.log(Level.INFO,"PageSource ::"+driver.getPageSource());
try {
logger.log(Level.INFO,"Driver going to wait now...");
driver.wait(100000);
logger.log(Level.INFO,"Driver came out of wait now normally...");
} catch (Exception e) {
logger.log(Level.INFO,"Driver came out of wait now exception::"+e);
}
logger.log(Level.INFO,"driver instance task completed " );
logger.log(Level.INFO,driver.getCurrentUrl());
logger.log(Level.INFO,driver.getCurrentUrl());
logger.log(Level.INFO,str);
driver.close();
Now when I debug this code I get my proper result but when I run this piece of code from servlet I get only the html content not the proper content which is excepted to come from the given link.
I also tried using the firefox driver and the same code works fine with it.
I also tried the same code with Web client but the same problem is coming.
Can anyone help me with this exception?
Thanks,
When you debug the code, you can halt at checkpoints. But you cannot stop browser from loading. It loads itself independently.
Htmlunitdriver pagesource returns all html contents as it cannot execute javascript completely.If the htmlpage contains Ajax, javascript calls then it shows the calls as it is .