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 + "})()");
Related
I am trying to console.log in chrome. just basic stuff.this is my html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>hello</h1>
<button onClick="clickme()">click</button>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
now my js file include
var name="abc";
console.log("hello " + name);
clickme=()=>{
document.body.style.backgroundColor="red";
alert(name);
console.log("hello " + name);
}
The alerts work. The background image gets change on button click but the console.log is not working. It works using node js and when used in edge browser.
Is there something missing.
Make sure you have Info checked in your Default-levels dropdown settings:
console.log() always returns undefined (correct me if I am wrong), but prints the value passed to it in the console.
https://codesandbox.io/s/p9zjw8lxrm
Here I have an alert and console.log. You can see alert is running, but console.log does not get to run until you close the alert.
var name = "abc";
console.log("hello " + name);
document.getElementById("btn").onclick = function() {
clickme();
};
const clickme = () => {
document.body.style.backgroundColor = "red";
alert(name);
console.log("hello " + name);
};
When you first run the problem, you should see an alert, then after would be the console.log
When you load up the page,
First you get hello abc
Then Alert box, you click yes
Then hello abc
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.
I'm trying to replace the context of a loaded element with the following code:
$("#menu1").load("./templates/menu.html");
var str = $("#menu1").html();
alert(str);
str.replace("[number]", "1");
$("#menu1").replaceWith(str);
But I always get an empty str, the menu1 gets loaded correctly with the menu.html content so I have no idea what's going on.
You need to test the string after the load - the way you have done it, it can be run whilst the load is still going. Try this:
$("#menu1").load("./templates/menu.html", function() {
var str = $("#menu1").html();
alert(str);
str.replace("[number]", "1");
$("#menu1").replaceWith(str);
});
More information
$("#menu1").load("./templates/menu.html", function(){
//complete
var str = $("#menu1").html();
});
Loading part isn't instant so you are trying to apply var str = $("#menu1").html(); before load() has time to complete
You need to do the task in callback function of load:-
For example :-)
<!DOCTYPE html>
<html>
<head>
<script data-require="jquery#*" data-semver="2.1.3" src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
Hello this is rachit 1 1 1 1.
<div id="menu1"></div>
<script>
$("#menu1").load("index.html", function() {
var str = $("#menu1").html();
alert(str);
str.replace("[number]", "1");
$("#menu1").replaceWith(str);
});
</script>
</body>
</html>
Plunker
I'm using HTML publisher in hope to have a html page with some javascript codes running on hudson. The HTML code is like this:
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../stringformat.js"></script>
<script type="text/javascript">
......
sql=String.format("/.csv? select from table where exchange=`ABC......")
</script>
</head>
However, after a successful build, my html page doesn't show what it suppose to, and as I check the error console, it says
Error: TypeError: String.format is not a function
I have put my stringformat.js into the top folder, as the HTML publisher doesn't seem to allow the file to contain anything other than HTML files.
Can anyone tell me why the String.format is not loaded properly? Thanks!
PS: stringformat.js is the file i got from
http://www.masterdata.se/r/string_format_for_javascript/
The code should be working properly as this piece of code works outside the hudson
try code:
<html>
<head>
<title>String format javascript</title>
<script type="text/javascript">
String.format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
}
var _myString = String.format("hi {0}, i'am {1}","everybody", "stackoverflower");
function show(){
alert(_myString);
}
</script>
</head>
<body>
<input type="button" name="btnTest" id="btnTest" value="Test string format" onclick="show();" />
</body>
</html>
I have a javascript function and I want to call the same function twice.
Once on page load and once on a button click.
I want the output as the function on page load will keep on running in backend and
display output and when button is clicked again function is called and its output
gets displayed.
Finally both functions output should be displayed.
This is what I have written but am not getting the output.
For e.g.
Javascript file: n.js
function webservice{
for(i=0;i<10;i++)
{
alert("some msg");
`enter code here`
}
html file n.html
<html>
<head>
<script type = "text/javascript" src="n.js"></script>
<script type="text/javascript">
window.onload=function() {
webservice();
}
</script>
</head>
<body>
<input id="button" type = "button" name = "webservice" value = "Call Webservice"
onClick="webservice()" />
</body>
</html>
Following code is working on my machine. I think you are missing () for function webservice(). so instead of webservice use webservice()
<html>
<head>
<script>
function webservice()
{
for(i=0;i<10;i++)
{
alert("some msg");
}
}
window.onload=function()
{
webservice();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input id="webservice" type = "button" name = "webservice" value = "Call Webservice" onClick="webservice()" />
</body>
</html>
Update:
if you write your onload function like this then you will be able to execute call 1 and call 2, butcall 2 will begin when call 1 loop is completed.
window.onload=function()
{
webservice('onload'); //call 1
document.getElementById("webservice").click(); //call 2
}
I think you code may be in wrong format.
function webservice{
for(i=0;i<10;i++)
{
alert("some msg");
`enter code here`
}
change to
function webservice(){
for(i=0;i<10;i++)
{
alert("some msg");
//enter code here
}
just add () after webservice. like webservice().