Does flutter(Dart) have a function similar to js bind?
I wanted to do something similar in flutter.
like:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
function handlerName(e)
{
alert(e.data.msg);
}
$(document).ready(function(){
$("p").bind("click", {msg: "chicked!"}, handlerName)
});
</script>
</head>
<body>
<p>click me!</p>
</body>
</html>
There is nothing like bind in Dart
but this might do what you want as well:
onReady.listen((e) => handlerName(e, msg: 'clicked'))
Related
after to make some research I took the measures to guarantee that the JS will be loaded before trying to run it but... it won't work!
This works:
<!DOCTYPE html>
<head>
<meta name="viewport" content="initial-scale=1" charset="utf-8">
</head>
<html>
<body>
<script type="text/javascript">
function initialize() {
console.log('foo')
}
window.onload = function() {
initialize();
};
</script>
</body>
</html>
But this doesn't:
<!DOCTYPE html>
<head>
<meta name="viewport" content="initial-scale=1" charset="utf-8">
</head>
<html>
<body>
<script type="text/javascript" src="test.js">
<script type="text/javascript">
window.onload = function() {
initialize();
};
</script>
</body>
</html>
test.js:
function initialize() {
console.log('foo')
}
I don't get any error. It just doesn't work. What am I doing wrong?
Thanks!
You need a closing script tag for the tag that is calling the external js file.
<script type="text/javascript" src="test.js"></script>
I have the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button onclick="this.innerHTML=Date()">The time is?</button>
</body>
</html>
This is working fine.
But if I try to do the same thing by creating a separate JavaScript function, the code is not working.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button onclick=displayDate()>The time is?</button>
<script>
function displayDate(){
this.innerHTML=Date();
}
</script>
</body>
</html>
What is the reason for this?
Your this isn't refer to the button itself, when it is in the function scope. You can achieve to the desired result with many approaches.
1) You can pass this to the function as a parameter
function displayDate(context){
context.innerHTML = Date();
}
<button onclick="displayDate(this)">The time is?</button>
2) Using explicit bindings, like call or apply
function displayDate(){
this.innerHTML = Date();
}
<button onclick="displayDate.call(this)">The time is?</button>
You can try to do something like this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button id="test" onclick=displayDate()>The time is?</button>
<script>
function displayDate(){
document.getElementById("test").innerHTML=Date();
}
</script>
</body>
</html>
How can I pass argument to function redirect in JavaScript
Here is my code:
<script type="text/javascript">
<!--
function redirectlink(text){
window.location = "index.php?keyName="+ text;
}
//-->
</script>
<form>
<button type="button" id="butt_1" onclick="redirectlink(KEY_POWER)"> 1 </button>
Thank you in advance.
This can be done either by using getElementById and addEventListener
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button id="foo">Click</button>
<script type="text/javascript">
document.getElementById("foo").addEventListener("click", function(){
window.location.replace("http://stackoverflow.com/q/36933820/5526354")
})
</script>
</body>
</html>
either onclick
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button id="foo" onclick="action()">Click</button>
<script type="text/javascript">
function action(){
window.location.replace("http://stackoverflow.com/q/36933820/5526354")
}
</script>
</body>
</html>
Using onclick is deprecated.
With just only JavaScript (without jQuery / Angular etc.) you can use addEventListener on click event.
for example:
var btn = document.getElementById('butt_1');
btn.addEventListener('click', function(e) {
// your code
});
In this function you can for example get value/txt from this button element and something else which you want.
I want to write the following code by using functions:
<!DOCTYPE html>
<html>
<head>
<title> Test Java Script</title>
</head>
<body>
<button onclick=this.innerHTML=Date();> The Time Is: ? </button>
</body>
</html>
So I created a function and pass "this" as parameter, but it doesn't work:
<!DOCTYPE html>
<html>
<head>
<title> Test Java Script</title>
</head>
<body>
<button onclick=setTime(this)> The Time Is: ? </button>
</body>
<script type="text/javascript">
function setTime(Object b) {
b.innerHTML=Date();
}
</script>
</html>
What am I doing wrong ?
Try this:
<!DOCTYPE html>
<html>
<head>
<title> Test Java Script</title>
<script type="text/javascript">
function setTime(b) {
b.innerHTML=Date();
}
</script>
</head>
<body>
<button onclick="setTime(this);"> The Time Is: ? </button>
</body>
</html>
Add quotes to onclick.
Remove "Object" from function declaration.
Click the button.
Just remove the Object type from your function parameter and you're golden:
function setTime(b) {
b.innerHTML=Date();
}
As Katana314 mentioned in their comment, JavaScript doesn't use explicit types.
Here's a fiddle: http://jsfiddle.net/3cyj5916/
Document.prototype.greenify = function(){
return {
style : function(){
return this.color = "green";
}
}
};
document.getElementsByTagName("H1")[0].greenify();
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>TEST</h1>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
Hey out there,
I wanna build a 'dot function'.
My function for trial should 'greenify' my element.
I already tried to add the function to Window- object but I came to the same result.
So, now my question...
What I did wrong or did I forget something?
I'm thankfully for each answer I receive :)
h1 elements don't inherit from Document.prototype. They inherit from those:
HTMLHeadingElement.prototype
HTMLElement.prototype
Element.prototype
Node.prototype
EventTarget.prototype
Object.prototype
For example, you can add the method to HTMLElement.prototype:
HTMLElement.prototype.greenify = function(){
this.style.color = "green";
};
document.getElementsByTagName("H1")[0].greenify();
HTMLElement.prototype.greenify = function(){
this.style.color = "green";
};
document.getElementsByTagName("H1")[0].greenify();
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>TEST</h1>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
However, note that modifying objects you don't own is considered a bad practice.