Trying to Output Math.Random(); Unsuccessfully - javascript

I'm using JSBin.com to test this simple code, for fun, but I can't seem to get it to output anything in the "Output" window on the site... is this just a limitation of the site or am I missing something completely obvious?
Here's my HTML-
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Math.Random</title>
</head>
<body onLoad "findRandom();">
<p id="getRandom"> </p>
</body>
</html>
And my JS-
function findRandom()
{
document.getElementById("getRandom").innerHTML = Math.random();
}

Missing an equal sign maybe
<body onLoad="findRandom();">

Related

JS Reference Error on calling function from link

I've been working on JS for just under 6 months now, but today when I tried to start a new project, I couldn't get a simple function to run from a link... I've searched around, simplified my code and still can't get it.
I'm getting a reference error, saying 'test' is not defined. Not really sure why as it's worked perfectly every other time as far as I can remember.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascipt">
function test()
{
alert("Entered");
}
</script>
</head>
<body>
<div>
<p id="data"></p>
<a onclick="test()" href="#">Fetch Data</a><br />
Fetch Data v2
</div>
</body>
</html>
At this point, I don't even know what to do...
Remove the type="text/javascipt" from your script tag, or correct spelling.
You have a spelling mistake with:
text/javascipt (should be text/javascript)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript">
function test()
{
alert("Entered");
}
</script>
</head>
<body>
<div>
<p id="data"></p>
<a onclick="test()" href="#">Fetch Data</a><br />
Fetch Data v2
</div>
</body>
</html>

jQuery: reading variable from external Javascript file

I'm stuck. I really don't know what I'm doing wrong, but I can't get a string returned to a variable in my embedded Javascript.
token.js:
function token () {
return "mysecretstring";
}
HTML Code:
<!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>TXTURE Server Status</title>
<script src="http://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
token_val="";
$.getScript('./token.js', function(data) {
token_val = token();
console.log("function: " + token_val);
});
</script>
</head>
<body>
</body>
</html>
I can do whatever I want, token_val remains empty. Any hints appreciated.
Best regards, Thomas
I do not use that way to reference a function js. I simply load the library js as same your loads jquery-1.12.4.min.js and then I make the call to the function token() from the script.
Code look like that:
<script src="path_to_project/tokens.js" type="text/javascript"></script>
<script type="text/javascript">
token_val = token();
console.log("function: " + token_val);
</script>
Could you try to change your URL argument from "./token.js" to "/token.js"?
I modified the code a little bit to get not confused by too many tokens. Somewhere in the middle I got the error "Unexcpected token error", which is somehow misleading when using a function with the same name. :)
Anyway, that's the code now which works.
Javascript:
function myaccesstoken() {
return "mysecrectstring";
}
HTML:
<script src="token.js" type="text/javascript"></script>
<script type="text/javascript">
let token_val = myaccesstoken();
</script>
Easy as that but somethimes you stare at walls without seeing the windows to your left ... anyway, thanks.
Regards, Thomas
Simply don't use your jQuery $.getScript(), but include the script that contains token() function with
<script type="text/javascript" src="/pathToScript">
And use the function

How TO - Include HTML

i want to include HTML Nav Bar, in Index HTML File.
(I prefer to follow suit) I do not know what wrong I did, but it does not show me the Nav bar..
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Home Page</title>
<script src="https://www.w3schools.com/lib/w3.js"></script>
</head>
<body>
<div w3-include-html="navbar.html"></div>
<script>
w3.includeHTML();
</script>
</body>
</html>
You can OR solve this with PHP, as #floreich mentioned, otherwise I see your code is correct but perhaps your path to the file is not correct.
I mean this line of code: <div w3-include-html="navbar.html"></div>.
Try to navigate to the correct path of your file called 'navbar.html', and it should work. Let me know! :)
This might help:
<div w3-include-html="dir/navbar.html"></div>
As you can see 'dir/' is added before 'navbar.html', which is a directory where 'navbar.html' is located.
Perhaps this explanation might help to understand above: https://www.w3schools.com/hTml/html_filepaths.asp
EDIT:
'dir/' is just a name, so change it to YOUR directory name.

phonegap-cordova - javascript , Hebrew value of strings not encoded correctly

Given the code:
<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap Application</title>
<script type = "text/javascript" src = "cordova.js"></script>
<script type = "text/javascript" charset = "utf-8">
function onBodyLoad() {
alert("שלום");
}
</script>
</head>
<body onload = "onBodyLoad()" >
</body>
</html>
I run this code as a phonegap app on my galaxy note 2 , but the result alert content is a gibrish code something like "OoO#^"
i added link to screenshot-
https://www.dropbox.com/s/d5juxri80o3qzsl/tempFileForShare.jpg?dl=0
thx.
i tried to open this page in ordinary browser and it's opened correctly.
so apparently the problem related to cordova.
It is look like encoding problem.
try to add this after head tag.
<head>
<meta charset="UTF-8">

What is the wrong with this dojo code?

This is just a piece of test dojo code I am writing. I am a beginner to Dojo and I can't understand what is wrong with this. The alert button is not displayed on load. Furthermore,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial: Hello Dojo!</title>
<!-- load Dojo -->
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js" data-dojo- config="async:true, parseOnLoad:true">
require(["dijit/layout/BorderContainer", "dijit/layout/TabContainer",
"dijit/layout/ContentPane", "dojo/parser"]);
require(["dojo/ready"],function(ready){
alert("BLAH");
});
</script>
<div id="targetID">
Hello world
</div>
</body>
</html>
Don't put javascript in the same tag where you have src= and close your script tags
It seems to be ok here: http://jsfiddle.net/XwAhY/ . Are u sure that data-dojo- config in your page is without spaces?

Categories

Resources