how to call javascript function from vbscript.
i wrote like this
<script type="text/vbscript">
jsfunction()
</script>
<script type="text/javascript">
function jsfunction()
{
alert("Hello")
}
</script>
but it is showing that type mis match how to achieve it. please help me.
Thank you,
Mihir
Assuming you want this client side as opposed to ASP;
If you place the JScript block before the VBScript block (or wire the call to a load event) that will work fine. (IE only of course)
...
<head>
<script type="text/vbscript">
function foo
call jsfunction()
end function
</script>
<script type="text/javascript">
function jsfunction()
{
alert("hello");
}
</script>
</head>
<body onload="foo()">
...
Calling a VBScript function from Javascript
Your VBScript:
Function myVBFunction()
' here comes your vbscript code
End Function
Your Javascript:
function myJavascriptFunction(){
myVBFunction(); // calls the vbs function
}
window.onload = myJavascriptFunction;
Alternatives (incompatible in some IE versions):
// This one:
window.onload = function(){ myVBFunction(); }
// This will also work:
window.onload = myVBFunction();
// Or simply:
myVBFunction();
// From a hardcoded link, don't write a semicolon a the end:
link
Inversed:
Calling a Javascript function from VBScript
Function myVBFunction()
myJavascriptFunction()
End Function
Try this ...
<%# Language=VBScript %>
<HTML>
<HEAD>
</HEAD>
<BODY>
<script language="JavaScript" >
function jsfunction()
{
alert("Hello")
}
</script>
<%
Response.Write "Calling =" jsfunction() "."
%>
</BODY>
</HTML>
Instead of alert we should write return which is in turn print the values on page using response.write-
code is below -
<%# Language=VBScript %>
<HTML>
<HEAD>
</HEAD>
<BODY>
<script language="JavaScript" runat="server">
function test() {
return "Test";
}
</script>
<%
Response.Write "Value returned =" & test() & "."
%>
</BODY>
</HTML>
Related
I've called two functions in my HTML
<body onload="myFunction()" onmousemove="myFunction1()">
I want to execute these two functions from a javascript file and call this file in my HTML like
<script type="text/javascript" src="path/to/functions.js"></script>
i.e. both the functions will be called from the javascript file, not from html.
I have tried
window.addEventListener('load', myFunction());
window.addEventListener('onmousemove', myFunction1());
or
$(window).load(function () {
myFunction();
});
$(window).onmousemove(function () {
myFunction1();
});
but failed.
What should I do ?
DETAILS
I have a javascript file where some functions are there.
script.js
function myFunction() {
alert("This is my function");
}
function myFunction1() {
alert("This is my second function");
}
I called these functions in my html body tag
<body onload="myFunction()" onmousemove="myFunction1()">
Now I want to call these two functions from my script.js like
window.addEventListener('load', myFunction());
window.addEventListener('onmousemove', myFunction1());
what should I do ?
I think you have understood my requirement.
Your code should read like this:
window.addEventListener('load' , myFunction );
window.addEventListener('mousemove' , myFunction1 );
But if it still does not work for you, Try this:
window.onload = myFunction ;
window.onmousemove = myFunction1 ;
Check this: onload and this: onmousemove
Try
function number1(){
document.querySelector("#info").innerHTML = "LOAD";
}
function number2(){
var x = event.clientX;
var y = event.clientY;
document.querySelector("#info").innerHTML = "MouseMove: ( x:"+x+" , y:"+y+") ";
}
window.onload = function(){
number1()
}
window.onmousemove = function(){
number2()
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="info">waiting</div>
</body>
</html>
I think You should verify that your (.js) file is loaded with the page or not?
Because i have tried both the example and both r working
window.addEventListener('load', myFunction); and window.addEventListener('load', myFunction());
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("Page is loaded");
}
window.addEventListener('load', myFunction);
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
or
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("Page is loaded");
}
window.addEventListener('load', myFunction());
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Hi I am trying to call the namespace JavaScript which is given in the internal JavaScript in below HTML representation.
<head>
<script type="text/javascript">
var ns = {
sampleAlert : function() {
ns.message(var );
}
message :function(var ) {
alert('msg');
}
}
</script>
</head>
<body>
<!--how to call the function sampleAlert-->
</body>
Is that possible? I am not able to call that namespace JavaScript function in the body.
Call it like this:
ns.sampleAlert();
Read this link to have more understanding on JavaScript Namespace
You probably need to do something like the following.
<head>
<script type="text/javascript">
var ns = {
sampleAlert : function(messageText) {
ns.message(messageText);
},
message : function(text) {
alert('msg ' + text);
}
}
</script>
</head>
<body>
<!--how to call the function sampleAlert-->
<script type="text/javascript">
ns.sampleAlert("this text will be displayed in the alert");
</script>
</body>
If i click button i want to call inside function hello.is it possible to call ?
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script>
function hai(){
alert("hai fun");
function hello(){
alert("hello function");
}
}
</script>
</head>
<body>
<button onclick="hello()">click here</button>
</body>
</html>
The functions defined within another function won't be accessible outside the function unless they have been attached to an object that is accessible outside the function.
function hai(){
alert("hai fun");
}
function hello(){
alert("hello function");
hai();
}
<button onclick="hello()">click here</button>
DEMO
set it to a variable:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script>
function hai(){
alert("hai fun");
whatYouAreLookingFor = function hello()
{
alert("hello function");
}
whatYouAreLookingFor();
}
</script>
</head>
<body>
<button onclick="hai()">click here</button>
</body>
</html>
Here is a way to call the inner function but it requires modifying the code of the outer function and passing in a variable to grab the inner function.
HTML
<body>
<button onclick="funcVariable()">click here</button>
</body>
Javascript
var funcVariable;
function hai(funcVar) {
funcVar = function hello() {
alert("hello function");
};
return funcVar
}
funcVariable = hai(funcVariable);
Pressing the button will now show the alert.
See JSFiddle
I am trying to set the attribute of the script tag for a infomous word cloud as follows:
<head>
function getParameterByName(name)
{
...
}
</head>
<body>
<script type="text/javascript"
async data-infomous-id="javascript:getParameterByName('wcid');"
id="embed"
src="http://www.infomous.com/client2/?width=800&height=600&maxWords=40">
</script>
</body>
But the javascript function is never executed. How can I set the data-infomous-id dynamically?
This should do this trick:
jsFiddle
var elem = document.getElementById('embed');
elem.setAttribute('data-infomous-id', getParameterByName('wcid'));
I added a javascript function in a page
<head>
<script type=text/javascript>
function show_Alert(error)
{
alert(error);
}
</script>
</head>
and on button click I am doing this
Protected void btn_Click(object o,Eventargs e)
{
StringBuilder str = new StringBuilder();
str.AppendLine("show_Alert('XYZ error')");
ClientScript.RegisterStartupScript(GetType(),"Alert",str.ToString(),true);
}
But it throws JS error show_Alert is not defined :(
Any Idea, what is wrong here??
Thanx
Your script tag is wrong.
Change it to
<script type="text/javascript">
However, I don't think that's the issue.
I suspect that RegisterStartupScript is emitting its <script> block before the one with your function, so that it ends up calling the function before it exists.
Check where each <script> is in the rendered source.
Make sure your <script> element is valid, like this:
<head>
<script type="text/javascript">
function show_Alert(error)
{
alert(error);
}
</script>
</head>
If it's not well formed, or the type is unrecognized (your case has both) then the script inside will be ignored, since the browser doesn't know how to handle it.
Here's an example:
<%# Page Language="C#" %>
<script type="text/C#" runat="server">
protected void BtnClick(object sender, EventArgs e)
{
var str = "XYZ Error";
var script = new StringBuilder();
script.AppendFormat("showAlert('{0}');",
HttpUtility.JavaScriptStringEncode(str)
);
ClientScript.RegisterStartupScript(GetType(), "alert", script.ToString(), true);
}
</script>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function showAlert(error) {
alert(error);
}
</script>
</head>
<body>
<form id="Form1" runat="server">
<asp:LinkButton ID="Btn" runat="server" Text="Click me" OnClick="BtnClick" />
</form>
</body>
</html>
A very important thing to pay attention to is to properly encode the string you are passing to the showAlert function. Notice that it is encoded:
script.AppendFormat("showAlert('{0}');",
HttpUtility.JavaScriptStringEncode(str)
);
If you don't encode it and the string contains some special characters like ' for instance, your script will break.