Transfer VB.NET variable to JavaScript - javascript

***VB.HTML
#Code
Dim SS_ID = Request.QueryString("SS_ID")
Dim initial_QRY
Dim ctrRecord
If SS_ID <> "" then
initial_QRY = "SELECT * FROM Tbl_rsSKUSetup WHERE SKUSetup_ItemID='" & SS_ID & "'"
ctrRecord = db.Query(initial_QRY).Count
If ctrRecord = 0 Then
System.Diagnostics.Process.Start("C:\Users\ushuam00\Documents\My Web Sites\EmptySite5\_Files\skuSetupGenericData.xlsm")
Else If ctrRecord > 0 Then
Else
End Code
***JavaScript
function alertCounter(){
alert('');
}
I want ctrRecord to be placed inside alter of JavaScript. Now I have the VBHTML on event "onload" calling alertCounter() All I have to see if I can capture ctrRecord so I can display it under alert.

I got this simple solution
display the ctrRecord inside the body where your VB.net code rest
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
#ctrRecord
</body>
</html>
then call JS via iFrame using onload event
Javascript
function alertCounter(){
alert(searchFrame.document.body.innerText);
}

Related

Selecting specific part of an HTML text

I have a function that returns some HTML fragment that I store in a variable called data, with its whole structure. What I want is to extract from it some of those parts.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script id="hello">
</script>
</head>
<body>
</body>
</html>
For example, I want to get the body and save it in a new variable:
var body = data.split("<body")[1].split(">").slice(1).join(">").split("</body>")[0];
Where data is the HTML text as a string that the original function is returning.
Is there any way I could save an specific script, from its ID (in this case with id = hello), and save it in another variable??
Thank you very much
var newVar = $("#hello").html();
Let's suppose you have an HTML string in a variable, for example
var foo = '<body><span>bar</span></body>';
Now, let's initialize a parser, to convert this into HTML:
var parser = new DOMParser();
var doc = parser.parseFromString(foo, "text/html");
Now, you can read anything from foo, as it is converted into HTML:
document.getElementsByTagName("body")[0].innerHTML = doc.querySelectorAll("body")[0].innerHTML;
$html = document.querySelector("body").innerHTML;
$hello = document.getElementById("hello").innerHTML;
console.log($html);
console.log($hello);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script id="hello">
// script data
</script>
</head>
<body>
</body>
</html>

Android web view load javascript in random order / not loading

I have an issue with a web view I'm using.
The javascript files used to render the view properly MUST be loaded in a specific order.
On browser (chrome mobile; safari desktop, ...) it works well.
However in my web view the result is unpredictable.
This is one of the latests failing run I had:
The HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/script1.js"></script>
<script src="js/script2.js"></script>
<script src="js/script3.js"></script>
<script src="js/script4.js"></script>
... More scripts...
</body>
</html>
Here is the Java code:
mWebView = (WebView)findViewById(R.id.webView);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setHorizontalFadingEdgeEnabled(false);
mWebView.setScrollbarFadingEnabled(false);
mWebView.setVerticalFadingEdgeEnabled(false);
mWebView.setOverScrollMode(View.OVER_SCROLL_NEVER);
mWebView.loadUrl("http://192.168.1.24:8000/path/to/index.html");
The issue is that randomly, javascript files are interpreted in wrong order or just not loaded.
From my understanding, javascript MUST be loaded in declaration order.
Am I wrong ?
Is there anything I can do to change that behavior ? (other than concatenating all JS together..)
Your scripts will work on the web site but for make it work on the android you need to load the scripts manually. See the function how it load the script from assets.
private void injectScriptFile(WebView view, String scriptFile) {
InputStream input;
try {
input = getAssets().open(scriptFile);
byte[] buffer = new byte[input.available()];
input.read(buffer);
input.close();
// String-ify the script byte-array using BASE64 encoding !!!
String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
view.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
// Tell the browser to BASE64-decode the string into your script !!!
"script.innerHTML = window.atob('" + encoded + "');" +
"parent.appendChild(script)" +
"})()");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
See Here Example
also make sure you enabled JS:
webSettings.setJavaScriptEnabled(true);
and the manifest ofcourse:
<uses-permission android:name="android.permission.INTERNET" />

priniting a pdf genrated from php from a iframe using js

I have this code
<!DOCTYPE HTML>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="lolkittens" />
<title>Untitled 2</title>
</head>
<body>
<p>Don't forget to print your document!</p>
<iframe src="genpdf.php" id="pdfDocument"></iframe>
<script>
function printPage() { print(); }
function printIframe(id)
{
var iframe = document.frames ? document.frames[id] : document.getElementById(id);
var ifWin = iframe.contentWindow || iframe;
iframe.focus();
ifWin.printPage();
return false;
}
printIframe('pdfDocument');
</script>
</body>
</html>
courtesy Open Printer Dialog for PDF file Automatically
However i keep getting this error ...
TypeError: ifWin.printPage is not a function
not too sure what i am doing wrong and how it can be solved (problem in FF and chrome) ..
thanks in advance.
Yes You need to put printPage function inside Iframe but there is one more way you can call print method of the window object of iframe directly without the need of printPage() like
ifWin.print();
instead of
ifWin.printPage();

Web-browser is not showing the output

I'm new to JavaScript and already encountered a problem. When I run the code and the browser pops up, it[browser] does not show anything. What I have is the testMethod.js file with one method:
function testMethod(num1, num2){
var value = num1 + num2;
return value;
}
and an HTML file from where I'm trying to run:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title> My JavaScript</title>
<script language = "javascript" src = testMethod.js"></script>
</head>
<body>
<script language = "javascript" type = "text/javascript">
// var getValue = testMethod(2,3);
document.write("The result is " + testMethod(5,3));
</script>
<noscript>
<h3> This site requires JavaScript</h3>
</noscript>
</body>
</html>
The code is not implementing the result at all. It shows only a blank page browser.
It seems you have a quote missing in the html, it should say src="testMethod.js" where you are including the script in the first place.

Android: Issue in Calling JavaScript Function

JavaScript Function
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>My HTML</title>
</head>
<body>
<h1>MyHTML</h1>
<p id="mytext">Hello!</p>
function callFromActivity(msg){
alert("Hello! I am an alert box!");
document.getElementById("mytext").innerHTML = msg;
}
</script>
</body>
</html>
Android Code
myWebView.loadUrl("file:///android_asset/mypage.html");
String msgToSend = "message";
myWebView.loadUrl("javascript:callFromActivity(\"" + msgToSend
+ "\")");
on Button Clicked event java script function call correctly. but i am not able to call java script function without button click.
Your javascript is wrong formatted. You should put it in such tags
<script type="text/javascript">
//your JS
</script>
So, your code will looks like that
<script type="text/javascript">
function callFromActivity(msg){
alert("Hello! I am an alert box!");
document.getElementById("mytext").innerHTML = msg;
}
</script>
try this ,
String msgToSend = "message";
String js = "msg = "+msgToSend+";"
+"function callFromActivity(msg){"
+"alert('Hello! I am an alert box!');"
+"document.getElementById('mytext').innerHTML = msg;";
view.loadUrl("javascript:(function(){" + js + "})()");

Categories

Resources