calling javascript function from body of html [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a javascrip[t function that is saved in a file (test.js). I want to call the function from within the body of the html file
something like
<script>
load drawLogo("text" val1, val2, val3);
</script>
(since this function will be called on different pages with different values)
Right now I am having to put that call at the bottom of the main function drawLogo() which means I can't customize it.

<script src="text.js" type="text/javascript"></script>
if it is in same function then make it like:-
onclick="func()" or onmmousehover="func()"
define function func() in the same file within script tag

Just put the function name and the parameters that receive this, dont forget first call the js file:
test.js:
function drawLogo(paramText,value1,value2,value3){
//Do something
}
html:
<head>
<script src="test.js" type="text/javascript"></script>
<!--you can put this in the head or in the body-->
<script>
drawLogo("some","value","for","example");
</script>
</head>
<body>
<!--you can put this in the head or in the body-->
<script>
drawLogo("some","value","for","example");
</script>
</body>

Related

how to get webpage refresh automatically every 10sec [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I do not know where to put the function
setTimeout('history.go(0);', 10000)
how do I code on a page?
One place you could put it is here:
<html>
<head>
<script>
setTimeout('history.go(0);', 10000)
</script>
<head>
<body>
..
</body>
</html>
another, better way is to write the script as follows
<html>
<head>
<script>
//console.info('initial load');
addEventListener('DOMContentLoaded', function () {
setTimeout(function () {
//console.info('reloading');
window.location.reload();
}, 10000);
})
</script>
<head>
<body>
my body
</body>
</html>
because this will wait for the whole document to be loaded before executing.
I would recommend reading up on AJAX and only fetching the parts you need as an asynchronous event, instead of loading the whole page again and again...
You don't even need a script...
Just add
<meta http-equiv="refresh" content="10">
in the headers.
Why do you try to do this via JS???
You need a timeout function to execute after 10 seconds (10000ms)
You can reload the page with window.location.reload()
setTimeout(function () {
window.location.reload();
}, 10000);

javascript onclick not calling the function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am currently trying to learn JavaScript. I have little experience in Web. Pardon me for this question.
I am trying to use a onclick event to a function.Unfortunately, it does not load the function.
<img id="btnPartDispatch" title="sometitle" class="img-rounded" src="~/Content/Images/dashboard/PartDispatch.png" width="115" height="115" onclick="HelloWorld()" />
function is in a different file:
function HelloWorld(){
alert"('hello world')"
}
However, if I change the onclick event to alert("Hello World") it works fine.
like this:
<img id="btnPartDispatch" title="sometitle" class="img-rounded" src="~/Content/Images/dashboard/PartDispatch.png" width="115" height="115" onclick="alert('Hello World!')" />
Can someone tell me what I do wrong?
Thanks
Getting the javascript files on this way:
#section scripts
{
<script type="text/javascript">
var secondsToClosePopup = 1000 * #Functions.GetNumberOfSecondsToClosePopup();
</script>
<script type="text/javascript" src="#Url.Content("~/Scripts/jquery.multiple.select.js")"></script>
<script type="text/javascript" src="#Url.Content("~/Scripts/service.js")"></script>
}
Project is ASP.net
You have to remove the double quotes from this:
alert"('hello world')"
in
alert('hello world')
your syntax for calling alert method is wrong. You can use it like this
<html>
<body>
<button onclick="HelloWorld()">Click me</button>
<script>
function HelloWorld() {
alert("Hello World");
}
</script>
</body>
</html>
Your alert"('hello world')" will throw syntax error, because it must be alert('hello world');
Why it must be?
alert or window.alert is a function that displays browser's default dialog box.
Since its a function, you will have to add () after its name to call it, and pass necessary arguments(message) that it expects.
Hence alert('hello world').
Also as a side note, since you are binding onclick in HTML, make sure that your function is loaded before DOM is loaded. Else it will throw error.

collapse and expand button in a google site [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm working on a Google site as I though it was easier...But I'm having some troubles with it. I'd like to create a button that expand and collapsea block of text. I've tried some different options but I didn't get it. Does anyone know how to do it?
Thanks a lot
EDIT: Something like this:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
<button>Toggle between hiding and showing the paragraphs</button>
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</body>
</html>
http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_toggle
For example, if you can use JavaScript, you can set the style to "display:none;" or "display:block;" by clicking a button.
<div id="ThisTextWillBeHidden">This text needs to be hidden</div>
<button type="button"
onclick="document.getElementById("ThisTextWillBeHidden").style.display='none';">
Click me to hide!
</button>
<button type="button"
onclick="document.getElementById("ThisTextWillBeHidden").style.display='block';">
Click me to show!
</button>
I assume Google Sites does not support scripts, so you will need to save this code somewhere else, like on your hosting or in a file storage service, and call it like this:
<iframe src="location of the file containing your code.html"></iframe>

To Do list in HTML/Javascript/jQuery [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to make a to do list in Javascript / HTML5. Here's my HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do </title>
<link rel="stylesheet" href="todolist.css">
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="To Do.js"></script>
</head>
<body>
<div id="list">
</div>
<form id="to_do_list">
<h2>What do you want to do? </h2>
<input type="text" id="task" name="task"><br>
<br/>
<button type="button" id="addToList"> Add to List </button> <br/>
</form>
</body>
</html>
I'm trying to figure out how to work Javascript that will form a list with the user's entries.
Sorry if this is easy, I am new to this.
JavaScript
window.onload = function () {
$("addToList").onclick = addTask;
}
var addTask = function() {
alert("You clicked it!");
}
You need to decide whether to use JQuery or Javascript for your script section.
It looks you have used Javascript, then switched to JQuery to reference an object then went back to Javascript. Try to stick to just one.
<script>
// this could be $(window).ready(function() { ...
window.onload = function () {
$("#addToList").click(addTask); // you need to point at the id #addToList
}
var addTask = function() {
var task = $("#task").val(); // references the value inside #task
$("#list").append("<p>"+task+"</p>") // makes a new <p> element
$("#task").val(""); // empties out #task
}
</script>
I'm not really sure what you're looking for, but this does what I think you want.
Also, I don't think you should use spaces in filenames ("To do.js")

Passing values from one page to another in JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In below code, the value is not passed from one page to another.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<script type="text/javascript">
function generateRow() {
var myvar = "testuser";
'<%Session["temp"] = "' + myvar +'"; %>';
window.location = "WebForm2.aspx";
}
</script>
<head>
<title></title>
</head>
<body>
<div runat="server">
<input id="Button1" type="button" onclick="generateRow()" value="button" />
</div>
</body>
</html>
What is the problem in above code?
You cannot set a session with Javascript. Your output JS file will be exactly what you wrote down.
You can do it like this:
window.location = "WebForm2.aspx?temp=" + myvar;
And in Webform2 you can do:
Request.querystring["temp"];
Global variables in one page are not kept by the next.
If you want this client-side, you have three options (at least):
Pass the variable to the next page on the query string, e.g.:
window.location = "WebForm2.aspx?myvar=" + encodeURIComponent(myvar);
Use a cookie (millions of examples online, I won't repeat them, bit of a pain to read the cookie).
Use client-side session storage or local storage, both of which are covered here. Here's the client-side session storage example:
// Setting
sessionStorage.myvar = myvar;
// Getting (on the next page)
var myvar = sessionStorage.myvar;
(sessionStorage is a global variable provided by the browser.)
This works on just about any browser you need to care about (IE8+, and modern versions of basically everything else).
Server-side, both option 1 and 2 would work, but 1 would make the most sense.

Categories