How can I get a vbscript function to run on body onload in all browsers?
This code (from w3schools) works in IE only:
<script type="text/vbscript">
function myFunction()
alert("Hello World!")
end function
</script>
</head>
<body onload="myFunction()">
I want to replace the alert with the code that is already written in vbscript and is working as is. Can this be accomplished using javascript?
Any help appreciated
Short answer is: you can't. To run cross-platform in the browser I recommend javascript.
Also, for code reference I would recommend MDN, rather than w3schools.
Just about everything can be accomplished with Javascript. My guess is that only IE supports VBScript in <script> tags, but I'm not positive on that.
Anyway, to do it in Javascript:
<script type="text/javascript">
function myFunction() {
alert("Hello World!");
}
</script>
The rest should be the same.
Related
I'm working on a cross-site scripting attack on an internet forum where links can be put in post like
[url]http://google.com[/url]
which then gets surfaced as
http://google.com
on the forum's thread. If possible, I want to use the technique of invoking JavaScript functions through the href, i.e.
<script type="text/javascript">
function sayHello ( )
{
alert("Hello");
}
</script>
Clicking here alerts "Hello"
but since I don't have any way of using predefined JavaScript functions, I'm wondering if I can put anonymous functions in there, e.g.
Clicking here alerts "Hello"
I've tested this out in Notepad with the simple page
<html>
<head>
</head>
<body>
<div>
<p>Clicking on this will alert "Hello""</p>
</div>
</body>
</html>
but it didn't work.
Any suggestions for me?
Use an IIFE:
Click here
Although there's not too much difference from doing this without the IIFE, just as top-level JS code:
Click here
The only significant difference is that you can declare local variables within the function.
I have written a simple quiz in javascript and it works fine in my browser of choice, but when I tested it in IE and FF the buttons don't work. I get a "ReferenceError: Option1button() is not defined"
even in this little bit of code I get the same error:
<!DOCTYPE HTML >
<html><head><title></title></head>
<body onload="loadPlayer();">
<script type="text/javascript">
function Option1button(){
document.getElementById("op1").style.display = 'none';
}
function loadPlayer() {
document.write("<div id=\"op1\"><button onclick='Option1button()'>choose</button> OPTIONS <br></div>");
}
</script>
</body>
</html>
You CANNOT use document.write anywhere except code that runs immediately.
If you are trying to defer it so that the button doesn't appear until the function is defined, just do this:
<body>
<script>function Option1Button() {...}</script>
<div id="op1">...</div>
Since scripts block the page from loading, the button won't appear until the function is ready to be called.
i'm using HTML code and i wan't to show un Alert Message or alert box, i don't know what it is called, but a message with a "OK" button.
i want to show this alert when the page is loaded.
How do i do this?
And how to do this with a title in the Alert box?
Do I need JavaScript? if no, How to do this without javascript?
Jonathan
Yes you need javascript. The simplest way is to just put this at the bottom of your HTML page:
<script type="text/javascript">
alert("Hello world");
</script>
There are more preferred methods, like using jQuery's ready function, but this method will work.
You can use a variety of methods, one uses Javascript window.onload function in a simple function call from a script or from the body as in the solutions above, you can also use jQuery to do this but its just a modification of Javascript...Just add Jquery to your header by pasting
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
to your head section and open another script tag where you display the alert when the DOM is ready i.e. `
<script>
$("document").ready( function () {
alert("Hello, world");
});
</script>
`
This uses Jquery to run the function but since jQuery is a Javascript framework it contains Javascript code hence the Javascript alert function..hope this helps...
you need a tiny bit of Javascript.
<script type="text/javascript">
window.onload = function(){
alert("Hi there");
}
</script>
This is only slightly different from Adam's answer. The effective difference is that this one alerts when the browser considers the page fully loaded, while Adam's alerts when the browser scans part the <script> tag in the text. The difference is with, for example, images, which may continue loading in parallel for a while.
If you use jqueryui (or another toolset) this is the way you do it
http://codepen.io/anon/pen/jeLhJ
html
<div id="hw" title="Empty the recycle bin?">The new way</div>
javascript
$('#hw').dialog({
close:function(){
alert('the old way')
}
})
UPDATE : how to include jqueryui by pointing to cdn
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.0.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
For making alert just put below javascript code in footer.
<script>
$(document).ready(function(){
alert('Hi');
});
</script>
You need to also load jquery min file.
Please insert this script in header.
<script type='text/javascript' src='https://code.jquery.com/jquery-1.12.0.min.js'></script>
<script type="text/javascript">
window.alert("My name is George. Welcome!")
</script>
<script>
$(document).ready(function(){
alert('Hi');
});
</script>
You can try this.
$(document).ready(function(){
alert();
$('#reportVariablesTable tbody').append( '<tr><td>lll</td><td>lll</td></tr>');
});
I know I am probably doing this wrong because if trying this through try coffeescript feature it works but surprisingly it doesn't emit any result on my example:
<!--http://f.cl.ly/items/1u3Q3W101U2T18162v0V/test.html-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Page Title</title>
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script>
</head>
<body>
<script type="text/coffeescript" >
document.write "<h2>TEST</h2>"
</script>
</body>
</html>
The document.write method doesn't seems to output anything to the body, in this case console.log works fine but not document.write
Even after trying to run the script with a onload handler like I use in javascript
var loaded = function(){
alert("hello");
}
document.addEventListener('DOMContentLoaded', loaded);
but then in coffeescript as
loaded = ->
alert "hello"
document.addEventListener "DOMContentLoaded", loaded
it seems neither the event method is being fired as opposed to javascript version
Anyone could help me find out what is happening?
Thanks
UPDATE
if running the console after the page is loaded I can get the following to work without problem:
CoffeeScript.eval('document.write "<h1>testing</h1>"')
but still wondering why the page itself is not showing automatically
Works on Firefox and Chrome but not in Safari
It seems the page is not showing if using Safari 5.0.3
I don't know anything about CoffeeScript, but don't use document.write. It is evil: http://javascript.crockford.com/script.html
Use createElement and appendChild/insertBefore instead:
var p = document.createElement("p");
p.innerHTML = "Lolz";
document.body.appendChild(p);
myDiv = document.getElementById("aDiv");
document.body.insertBefore(p, myDiv);
document.write has problems in Safari as well.
This is a humdinger, but after investigating, I've got your answer:
The way coffee-script.js works is that it looks for, and runs, scripts with type="text/coffeescript" after the document has loaded. In the case of Safari, that means that
<script type="text/coffeescript">
document.write "<h2>TEST</h2>"
</script>
is equivalent to
<script type="text/javascript">
window.addEventListener('DOMContentLoaded', function() {
document.write("<h2>TEST</h2>");
}, false);
</script>
which silently fails. Note that making the insertion with the document.createElement method described by Erlend, or with a library like jQuery, will work fine.
Since this works in Chrome, I'd go ahead and call it a Safari bug. But the real moral of the story is: Don't use document.write.
How do I pop up an alert in JavaScript?
I tried alert"HELLO" but that didn't work.
Can someone tell me the correct syntax?
You need to add parentheses:
alert("HELLO");
Unlike VBScript, JavaScript requires parentheses around function calls.
Therefore, you need to write alert("Hello!");
It's also preferable (but not required) to end every statement with a semicolon ;.
Finally, if you aren't already, you need to put it in a script block, like this:
<script type="text/javascript">
alert("Hello!");
</script>
You can put JavaScript inside a script tag in an HTML page, or you can make a standalone .js file and double-click on it (in Windows) to run it using WSH.
<script type="text/javascript" >
alert("hello");
</script>
if you're having problems with alerts, it probably means your javascript is disabled in your browser, but if it isn't these methods should solve your issue.
<script type="text/javascript" >
window.alert("Hello World!");
alert("Hello World!");
</script>
Just add parantheses
alert("Hello");
<script type="text/javascript">alert("Hello");</script>
There are a few ways.
Use your web browser, type this code in address bar.
javascript:alert("Hello World!")
Open web developer in your web browser, go to console and type:
alert("Hello World!")
Type the code in .html or .js code as shown in other answers above.
Just add parentheses and enclosed it within the script tag.
<script type="text/javascript" >
alert("hello");
</script>
<html>
<head>
<script type="text/javascript">
function myFunction(){
alert("hello");
}
</script>
</head>
<body>
<input type="submit" onClick="myFunction()">
</body>
</html>
Here is the JavaScript function which has an alert box. This function is called by clicking on the submit button.