NameSilo

Simple Javascript not working in Firefox

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
Watch

gamex

Established Member
Impact
7
I have the following javascript:

PHP:
<script type="text/javascript">
function getPreview(form){
	link = form.link.value;
	document.imgpreview.location.href("imagepreview.php?url="+link);
}
</script>

I call it using this:

PHP:
<input type="text" name="link" onChange="javascript:getPreview(this.form)" size="40">

Works in IE, not in Firefox. I know with javascript, you can use shortcuts (like omitting the document in document.imgpreview and so on). I thought maybe that would be the problem, but in no way am I a javascript expert. Anyone have any idea?

FYI, imgpreview is the name and id of an iframe that loads a preview of the image typed into the text area.
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Code:
<input type="text" name="link" onchange="getPreview(this.form);" size="40">
^ try that instead. 'javascript:' is for using embedding it in a link.
 
0
Close
No way of testing this atm, but:

Code:
<input type="text" name="link" onChange="getPreview(this.form)" size="40">
Try this instead? Don't think you need to add "javascript:"

lol, always happens to me, someone posts before I do.. Jim_ :p
 
0
Close
I originally had it that way, but it didnt work. I added javascript: thinking maybe that was the problem.

I added a button as follows:

<input type="button" value="Image Preview" onClick="getPreview(this.form)">

Which I would click, thinking maybe the onchange thing didnt work, but that doesnt work either. So i am thinking its something with my function, but I cant figure it out. Am I getting the value of the link textbox correctly?

Hmm, I just thought of something, and I added this: alert("doh");to my javascript function, and I get the alert when I test it, meaning the function is executing, but either its not passing the value to the url (meaning the url= is blank, so the iframe content wont change), or its not changing the url of the iframe at all. Any ideas?
 
0
Close
function getPreview(form){
link = form.link.value;
alert(link);
document.imgpreview.location = "imagepreview.php?url="+link;
}

see what that returns.
 
0
Close
The alert box returns the value entered in link. So based on that, the problem is loading the page in the iframe, which looks like this:

PHP:
<iframe name="imgpreview" id="imgpreview" src="imagepreview.php" style="width: 400px; height: 150px; border: 1px solid #000000; background-color: #FFFFFF" scrolling="Auto" marginheight="0" marginwidth="0" frameborder="0"></iframe>

Im still clueless
 
0
Close
tip: the javascript console under "Tools" on firefox might help you debug it.
 
0
Close
The Firefox JavaScript console says this:

Error: document.imgpreview has no properties
Source File: content_image.php
Line: 8

If that helps at all



UPDATE:

figured it out.

document.imgpreview.location = "imagepreview.php?url="+link;

should be changed to

parent.imgpreview.location = "imagepreview.php?url="+link;

thanks for the help
 
Last edited:
0
Close
If you're using XHTML, make sure to make your attributes all in lowercase.

-Steve
 
0
Close
Code:
<script type="text/javascript">
function getPreview(form){
    link = document.form.link.value;
    document.imgpreview.location.href("imagepreview.php?url="+link);
}
</script>

You didn't use document in link.

Another way you could do it is by using getElementById(), that's normally my prefered method.
 
0
Close
CatchedCatched

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back