PDA

View Full Version : why doesn't this work? calling javascript from a button


vandenberg
12-21-2002, 12:21 AM
<input type="button" value="test2.html!" javascript:"alert('bla');">

I want my javascript to be started from the input tag, but it has got nothing to do with it really. It's no onClick javascript, it should start in any case.. Why doesn't it work? I know it's a strange question but please help..

RadarBob
12-21-2002, 12:30 AM
when imbedded in a tag like that you need an event to trigger it.

If you want to have it just execute w/out any triggering, make the code "in-line", like this:

<input type="button" value="test2.html!">
. . .
</form>

<script type="text/javascript">
alert('bla');
</script>

Now it will "just execute" as the browser gets to that line as the page loads.

vandenberg
12-21-2002, 12:34 AM
Radarbob, thanks but I need to write it into the <input> tag.. Is there any way to do that? Don't ask me why. I understand that it is better to do it the way you showed me but I need this anyway :) I always thought it works with "javascript:" but it doesn't


edit: ah... I need an event to trigger it.. Without one no way to make it work?

vandenberg
12-21-2002, 12:38 AM
I tried

<input type="button" value="test2.html!" window.onload="alert('bla');">

but it doesn't work aswell *scratches head*

ez4ne12c
12-21-2002, 12:40 AM
The javascript wont run without an event from an input tag.

you could put the offending code in an onLoad event in your body or just put the code into the page as a script

but you wont be able to get it to run from inside an input without some event.. that doesnt make sense... to me
ez

vandenberg
12-21-2002, 12:43 AM
I know it doesn't make sense.

I need it anyway.

I just don't understand why it doesn't work this way:
<input type="button" value="test2.html!" window.onload="alert('bla');">

I mean it says window.onload :confused:

vandenberg
12-21-2002, 12:48 AM
so, is it really impossible to call it without an event?

If yes, can you give me an event, that in most of the cases happens? window.onload doesn't work.... :(

ez4ne12c
12-21-2002, 01:11 AM
The onLoad event always happens but you need to define this in the body tag, not in an input tag... why do you want to do this in an input tag.. it doesnt make sense tome?
ez

either
<body onLoad='alert("bla")'>
...
<input ..
...
</body>


or
<body>
...
<input ....>
...
<script>
alert ('blah');
</script>
...
</body>

will work
ez

vandenberg
12-21-2002, 01:18 AM
Maybe I'll ask the question so it does make sense:

What if I want to resize an image with javascript?

<img src="bla.gif" this.width="100">
that doesn't work.. why?

thank you very much for your help :)

krycek
12-21-2002, 01:21 AM
as has been said, you need to call the code from an event. Events are different for different things... window.onLoad only applies to the window object, not to an input.

Try this:

<input type="button" value="test2.html!" onClick="alert('bla');">

That will pop up your message every time someone clicks on your button.

Experiment by replacing onClick with these:

onMouseDown
onMouseUp
onMouseOver
onMouseOut

and, to add variety, try these too:

onFocus
onBlur
onChange

MSDN has a list of events, but they only apply to IE. So, not all of them will work cross-browser. You might want to look anyway, to get an idea of what is going on.

http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events.asp

I think that is nearer to what you want than calling the alert from the onLoad event.

Hope that helps! :)

::] krycek [::

ez4ne12c
12-21-2002, 01:26 AM
To resize an image theres many ways

The simplest is to use innerHTML to change the IMG tag so that the image is rerendered to the new size by the browser.

This is the simplest solution cause you let the browser resize..
this is not always the best solution because of the technique used by the browser, line diagrams often drop data (pixels)

If you need to 'properly' resize an image you may need to use perl
then use something like ImageMagick or netpbm to resize the image for you...

If it is not your image you want to display you can use perl
to create a http socket to grab any image on the web..
then resize it..

ez

RadarBob
12-23-2002, 01:42 PM
Yo, Vandy;
You need to differentiate between how you wish you could write it and how one must write it to make it work. Restate what you want to happen, NOT how you wish you could code it. Then we can tell you how to do it. But it sounds like you want something to happen during page load. So what do you care if that goes in the body tag or in-line, but not in the input tag?

When you put javascript in a tag it must be tied to an event. Then it must be an event "supported" i.e. recognized, by that tag. "Window.onload" is not an event or one that input tags recognize. "ONLOAD" is in that category too. ONLOAD is associated with the <body> tag. Wishing won't change that.


Good Luck!

Kalexandert
12-23-2002, 02:39 PM
Hey Hey, I'm new to THIS forum but I thought I could help.
For one not all events are supported in all tags, Example "window.onLoad" is not an event of an input tag, it's an event of the body tag, or to be used in plain script in the head, of body of page.

To activate your script from teh input tag you would use.

<input type=text value=clickMe onClick="javascript:alert('blah');">

Try that!!

It should work.......

Thanks!

krycek
12-23-2002, 02:58 PM
it seems to me that everyone is now simply repeating what has already been said.

Like, Kalexandert, welcome to the forums, but seriously you didn't say anything different in your post to what I said in mine!

I think that vandenberg has to tell us exactly what is required... should the action fire from the input, upon a certain event, or when the document loads, or what?

Plenty of answers have been given addressing both of these, so there should be enough for vandenberg to work things out, unless we have all missed the point?

::] krycek [::