Calling Javascript function without click event

Grazehell

2[H]4U
Joined
Jun 13, 2001
Messages
2,660
I found a Javascript function that does everything I need to and would potentially replace my 15 lines of PHP code. Problem is I know nothing about JS.

The function is called upon click and reads the input from a text box and passes that parameter to the function.

I was wondering if I could have the function called without a click event. Says like when that line of code it read instead. I am not going to be using the input from the text box but directly pass a predefined variable instead

Anyone with the JS know how on doing this?

original call

Code:
onclick="DoSomething(document.frmPreselect.txtName.value)"
 
It sounds like you want it to load automatically without any user interaction. That happens when the page is loaded. There's a few implementations of that but the easiest is "when the dom is ready".

jquery offers a cross browser dom ready event. If not you'll have to implement it yourself, there's a lot of examples if you google for it.

http://stackoverflow.com/questions/1206937/javascript-domready
 
You may want to keep your PHP code. I don't know your situation at all but remember to never trust the client (validate all input sent to the server) and also keep in mind clients that do not have javascript enabled.

ShoeLace's answer is probably what you want, you can also use a one-off timed interval event to call your function as well.
 
I'm not 100% certain about what you're trying to accomplish, but if you're trying to preserve a value between client<->server you could also use hidden fields for your html forms. In that case you would initialize the value from your PHP, and the value would be with the form or whatever when it gets back to the server.

In any event, I'm also inclined to agree with blackrose that values should be set and validated by the back-end PHP code.
 
You may want to keep your PHP code. I don't know your situation at all but remember to never trust the client (validate all input sent to the server) and also keep in mind clients that do not have javascript enabled.

ShoeLace's answer is probably what you want, you can also use a one-off timed interval event to call your function as well.

It's a small crud program that is run on a LAN and not exposed to the internet. I also take other precautions to prevent any SQL injection.


Code:
(function() {
  doSomething(yourVar);
})();

Yep this is exactly the code I used!!


I'm not 100% certain about what you're trying to accomplish, but if you're trying to preserve a value between client<->server you could also use hidden fields for your html forms. In that case you would initialize the value from your PHP, and the value would be with the form or whatever when it gets back to the server.

In any event, I'm also inclined to agree with blackrose that values should be set and validated by the back-end PHP code.

This is what I am doing but it is in regards to a listbox. I was using PHP to load the value that is in the database and my code just seems very ungraceful compared to just a few simple lines of Javascipt.
 
You could do it like that too but I think that's really sloppy.

It will execute no matter what state the dom is in. If you try to change a select box before it's even in the document then you'll get an error and the script won't work.
 
Back
Top