Hi I am trying to call a function inside the <script> tag.
The formatLink function it is not being called. All other functions in jms.js can be called. How can i make it call functions in both jms.js and the one i declare in the script tags..
<script type="text/javascript" src="javascript/jms.js">
function formatLink(cellvalue) { return cellvalue+"working";}
</script>
Write it like this:
<script type='text/javascript' src='javascript/jms.js'></script>
<script type='text/javascript'>
function formatLink(cellvalue) { return cellvalue+"working"; }
</script>
Each script have to be in it's own <script> tag:
<script type="text/javascript" src="javascript/jms.js"></script>
<script type="text/javascript">
function formatLink(cellvalue) {
return cellvalue+"working";
}
</script>
Related
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 have the following code:
<script language="javascript" type="text/javascript">
$.fn.dataTableExt.oPagination.input = {
"fnInit": function (oSettings, nPaging, fnCallbackDraw) {
....
},
"fnUpdate": function (oSettings, fnCallbackDraw) {
....
}
};
</script>
and it works, but when I create external file and put this code inside this file (of course without <script></script> ) - it does not work. Why?
just make sure the jQuery is the first script you load
<script type="text/javascript" src="jquery.js"><scrip>
<script type="text/javascript" src="yourScript.js"><script>
I'm trying to reuse a function i made, but with any help yet.
Here is my index.html.
<html>
<head>
<script src="jquery-1.5.1.js")" type="text/javascript"></script>
<script src="JqueryScript.js")" type="text/javascript"></script>
<script type="text/javascript">
//MYLIBRARY.init(['#button1']);
execute(['#button1']);
execute(['#button2']);
</script>
</head>
<body>
<button type="button" id="button1">Click Me!</button>
<button type="button" id="button2">Click Me!</button>
</body>
</html>
And here is my script.
function execute (Args){
_args = Args;
$(function() {
$(_args[0]).click(function() {
alert("hello " + _args[0]);
return false;
});
});
}
The problem is always triggers the second alert windows (#button2). How can i make it work with both alert windows?
My guess is that you are wondering why both buttons alert the same output.
Because _args is global. Thus the second invokation will overwrite _args.
Use var to make it local:
var _args = Args;
Your code could still be improved (see #Town's answer), but this should get you started.
You also have errors in your HTML
<script src="jquery-1.5.1.js")" type="text/javascript"></script>
<!-- ^^ -->
<script src="JqueryScript.js")" type="text/javascript"></script>
<!-- ^^ -->
What exactly are you trying to do?
From what I can see, you could achieve the same functionality as what you have with simply:
$(function() {
$('button').click(function() {
alert("hello #" + this.id);
});
});
Example
Looks like C to me :)
You'd be better off with
<script type="text/javascript">
$(function () {
execute('#button1');
execute('#button2');
}
function execute(buttonId)
{
$(buttonId).click(function () { alert('Hello ' + this.id); });
}
</script>