By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,812 Members | 2,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,812 IT Pros & Developers. It's quick & easy.

Problem accessing hidden variable in FIREFOX

P: n/a
Hello all,

In some hidden variable (<input type="hidden" name="hiddenId"
value="test" /> ,i stored some value.I accessed the value "test" using

var id = document.getElementById( 'hiddenId' );

It is working fine in IE. But in Mozilla Firefox , null value is
returned.Is there any way to use hidden variables compatible to
browsers......?

I don't know browser compatibility in javascript.Can anyone help me to
tackle this problem.........

Thanks,
karthik

Jan 13 '06 #1
Share this Question
Share on Google+
19 Replies


P: n/a
k.*********@gmail.com said the following on 1/13/2006 2:43 AM:
Hello all,

In some hidden variable (<input type="hidden" name="hiddenId"
value="test" /> ,i stored some value.I accessed the value "test" using
Thats not a hidden variable, its a hidden input.
var id = document.getElementById( 'hiddenId' );
How is Firefox supposed to find it by ID when the input has no ID? IE
incorrectly grabs it based on the NAME attribute.
It is working fine in IE. But in Mozilla Firefox , null value is
returned.Is there any way to use hidden variables compatible to
browsers......?

I don't know browser compatibility in javascript.Can anyone help me to
tackle this problem.........


Either give your input an ID, change to the document.forms collection:

document.forms['formNAMEnotID'].elements['elementNAMEnotID'].value;

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 13 '06 #2

P: n/a
k.*********@gmail.com wrote :
Hello all,

In some hidden variable (<input type="hidden" name="hiddenId"
value="test" /> ,i stored some value.I accessed the value "test" using

var id = document.getElementById( 'hiddenId' );

It is working fine in IE.
IE incorrectly implements getElementById :

"getElementById: Returns the Element whose ID is given by elementId. If
no such element exists, returns null." DOM 2 Core getElementById
http://www.w3.org/TR/DOM-Level-2-Cor...ml#ID-getElBId and DOM 3
Core getElementById
http://www.w3.org/TR/DOM-Level-3-Cor...ml#ID-getElBId

getElementsByName: With [HTML 4.01] documents, this method returns the
(possibly empty) collection of elements whose name value is given by
elementName. DOM 2 HTML getElementsByName
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-71555259
But in Mozilla Firefox , null value is returned.
Which is correct per DOM 2 Core.

Is there any way to use hidden variables compatible to browsers......?


var strInputValue = document.forms["FormName"].inputName.value;
or
var strInputValue = document.forms["FormName"].elements["inputName"].value;

More on this:
Using Web Standards in Your Web Pages
Accessing Elements with the W3C DOM
How to convert IE-specific ways to access elements into W3C web
standards replacements
http://www.mozilla.org/docs/web-developer/upgrade_2.html#dom_access

For these kinds of issues, check first
Mozilla Web Author FAQ
http://www.mozilla.org/docs/web-developer/faq.html

or the comp.lang.javascript FAQ first
comp.lang.javascript FAQ - 8.1 - 2005-11-05
http://jibbering.com/faq/

Gérard
--
remove blah to email me
Jan 13 '06 #3

P: n/a
Thanks for the reply.

I used "id" in hidden variable and its working.
Now i had another problem.
I have an IFrame whose onLoad event calls a javascript. In that script
i used the following to get the hidden variable value(in parent).

var Type = aspxIFrame.parent.document.getElementById("Type");
Here aspxIFrame is the ID of that IFRAME.

This also not working in firefox.

Error: aspxIFrame.parent has no properties
Source File: http://192.168.1.12/_layouts/1033/SCRIPT.js
Line: 7

How to solve this problem..?

Thanks,
karthik

Jan 13 '06 #4

P: n/a
VK

k.*********@gmail.com wrote:
I used "id" in hidden variable and its working.
Be aware that it will not work for form submital - only form elements
with "name" attribute set are being submited (though for addressing on
the page you can use either id or name). Also form element labels works
only for id's but not names. So I would suggest to take as a habit to
double identify form elements (but only form elements!).
....
<input name="Data001" id="idData001">
....
where "data001" will be the name sent to the server, and "iddata001"
will be the id you can use with document.getElementById() and <label
for="idData001" acceesskey=hotkey>

- That's redicilous!
- Totally agree, but that was the price to pay to incorporate modern
DOM stuff into such anscient and legacy heavy thing as HTML form.
var Type = aspxIFrame.parent.document.getElementById("Type");


In iframe:
var Type = self.ownerDocument.getElementById('idType');

Where hosting page contains:
<input type="hidden" name="Type" id="idType">

P.S. Unless you do not plan/want submit this field to the server, then
<input type="hidden" id="Type">

Jan 13 '06 #5

P: n/a
Once again thanks for the reply.

In IFRAME , i have one aspx page.In that page i show and hide some
elements using

var Toolbar = aspxIFrame.document.getElementById('ToolbarID');
if(Toolbar != null)
{
Toolbar.style.visibility = 'hidden';
}

This is not working in firefox.so i tried something like

var iframeObj = parent.document.getElementById('aspxIFrame');
if(iframeObj != null)
{
var Toolbar = iframeObj.document.getElementById('ToolbarID');
}

this returns null in IE and no properties error in firefox.

How to get those element by Id..........?

Jan 13 '06 #6

P: n/a
VK

k.karthi...@gmail.com wrote:
Once again thanks for the reply.

In IFRAME , i have one aspx page.In that page i show and hide some
elements using

var Toolbar = aspxIFrame.document.getElementById('ToolbarID');
if(Toolbar != null)
{
Toolbar.style.visibility = 'hidden';
}

This is not working in firefox.so i tried something like

var iframeObj = parent.document.getElementById('aspxIFrame');
if(iframeObj != null)
{
var Toolbar = iframeObj.document.getElementById('ToolbarID');
}

this returns null in IE and no properties error in firefox.

How to get those element by Id..........?


Sorry I've got lost who's Joe, but it's not your fault.

Just a common answer:

page "MainPage" contains iframe "MyFrame" (so no real frameset, just
MainPage with floating iframe object in it).

To address something in MainPage from within MyFrame:
var foo = self.ownerDocument.getElementById('bar');

To address something in MyFrame itself from within MyFrame:
var foo = document.getElementById('bar');
// thus no any extra commands

Jan 13 '06 #7

P: n/a
I am not calling javascript from MyFrame.Instead , i call script in
iframe onLoad().

so document.getElementById('bar');

gives value if that ID is in MainPage.But i want ID that is in MyFrame.

Jan 13 '06 #8

P: n/a
VK

k.*********@gmail.com wrote:
I am not calling javascript from MyFrame.Instead , i call script in
iframe onLoad().


That is a very bad idea, because how can you predict that by the time
MyPage fired "load" event , MyFrame is ready for scripting too? You
have absolutely no guarantees of that...
Oh wait, is it what you are trying to do? Check is a iframe variable is
available and based on that guess that iframe is loaded? That doesn'r
work anyhow reliable for too many reasons to spell. With frames/iframes
you *have* to go from bottom-to-top: not the main page is asking its
frames, but frames informing the main page.

But as you are the boss of your project, then here is:

from MainPage:
var obj = self.frames["MyFrame"].getReference("bar");

and in MyFrame (where "MyFrame" is *name*, not id):
function getReference(id) {
return document.getElementById(id);
}
This twisted way is needed to bypass Internet Explorer "same-window
methods scope" rule. Otherwise you can (or can not) get 5011 "Can't
execute code from a freed script" error.

But really sooner or later you'll realize that the only way to do such
things is from bottom-to-up. So why do not accept it right away? ;-)

Jan 13 '06 #9

P: n/a
I cant write Javascript inside MyFrame(aspx displayed in iframe).Those
pages are not in my control.
Depending upon the url i assigned the src of iframe.
Is there any other way.........

Jan 13 '06 #10

P: n/a
VK

k.*********@gmail.com wrote:
I cant write Javascript inside MyFrame(aspx displayed in iframe).Those
pages are not in my control.


Are they in the same domain? Means that for URL like:

http://www.myserver.com/folder/page.aspx

*every single character* up to "/folder" has to be the same:
"http://www.myserver.com"

Again: it is not important at all if this is the same server or not:
*every single character in the indicated part* has to be the same. If
at least one character differs then the only thing you can do with
iframe content is to set .src property (but you cannot read it, even if
you set it yourselve). Only this and nothing else.
For anything atop of it you have to be able to change the file which
you're loading into iframe.

Jan 13 '06 #11

P: n/a
k.*********@gmail.com said the following on 1/13/2006 6:04 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
Thanks for the reply.
Thank you for quoting in the future.
I used "id" in hidden variable and its working.
Now i had another problem.
I have an IFrame whose onLoad event calls a javascript. In that script
i used the following to get the hidden variable value(in parent).

var Type = aspxIFrame.parent.document.getElementById("Type");

Here aspxIFrame is the ID of that IFRAME.

This also not working in firefox.


Because you are not using a proper reference.

If the variable you want access to is in the parent window and you want
to access it from the IFrame then you reference it as:

parent.variableName

So in your case it would be:

Type = parent.document.getElementById("Type").value;

But I wouldn't use the same names there. Use something different:

thisType = parent.document.getElementById('parentType').value ;

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 13 '06 #12

P: n/a
VK said the following on 1/13/2006 6:36 AM:
In iframe:
var Type = self.ownerDocument.getElementById('idType');


Do you ever test code that you post?

In iframe:

alert(typeof self.ownerDocument);

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 13 '06 #13

P: n/a
VK said the following on 1/13/2006 10:57 AM:
k.*********@gmail.com wrote:
I cant write Javascript inside MyFrame(aspx displayed in iframe).Those
pages are not in my control.

Are they in the same domain? Means that for URL like:

http://www.myserver.com/folder/page.aspx

*every single character* up to "/folder" has to be the same:
"http://www.myserver.com"


Who taught you that garbage?

If they are from different sub-domains you simply have to set the
document.domain property.

If they are from different domains themselves then you have an issue.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 13 '06 #14

P: n/a
VK

k.*********@gmail.com wrote:
Those pages are not in my control.
Randy Webb wrote: If they are from different sub-domains you simply have to set the
document.domain property.


Who speak (or doesn't) English here?

Or you think that one can set document.domain from the main page? Like
"first I loaded it, now let me give some rights to myself"? ;-)

Jan 13 '06 #15

P: n/a
VK

k.*********@gmail.com wrote:
I cant write Javascript inside MyFrame(aspx displayed in iframe).Those
pages are not in my control.
Depending upon the url i assigned the src of iframe.
Is there any other way.........


OK, try the old fashionned way (but remember that it will fail for a
number of reasons in the real web):

# frame1.html
<html>
<head>
<title>MainPage</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>

<body onload="
alert(self.frames[0].document.getElementById('foobar').innerHTML);
">
<p>Main Page</p>
<iframe src="frame2.html"></iframe>
</body>
</html>

# frame2.html
<html>
<head>
<title>MyFrame</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>
<body onload="
alert(self.parent.document.documentElement.innerHT ML);
">
<p id="foobar">My Frame</p>
</body>
</html>

Jan 13 '06 #16

P: n/a
Randy Webb <Hi************@aol.com> writes:
document.forms['formNAMEnotID'].elements['elementNAMEnotID'].value;


Actually, that should probably be
document.form['formID']...
in modern HTML. HTML 4.01 recommends using the id attribute instead
of the name attribute:
---
Note. This attribute has been included for backwards
compatibility. Applications should use the id attribute to identify
elements.
---
(about name attribute on form element).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 14 '06 #17

P: n/a
Lasse Reichstein Nielsen said the following on 1/13/2006 8:58 PM:
Randy Webb <Hi************@aol.com> writes:

document.forms['formNAMEnotID'].elements['elementNAMEnotID'].value;

Actually, that should probably be
document.form['formID']...
in modern HTML. HTML 4.01 recommends using the id attribute instead
of the name attribute:
---
Note. This attribute has been included for backwards
compatibility. Applications should use the id attribute to identify
elements.
---
(about name attribute on form element).


Does that apply only to the form or to its elements as well? I am pretty
sure it would be the form only?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 14 '06 #18

P: n/a
Lasse Reichstein Nielsen wrote:
Randy Webb <Hi************@aol.com> writes:
document.forms['formNAMEnotID'].elements['elementNAMEnotID'].value;


Actually, that should probably be
document.form['formID']...
in modern HTML. [...]


It should probably be

document.forms['formID']...

there.
PointedEars
Jan 14 '06 #19

P: n/a
Randy Webb <Hi************@aol.com> writes:

[name attribute on form only for backwards compatability]
Does that apply only to the form or to its elements as well? I am
pretty sure it would be the form only?


Form only.

For the form controls, the "name" attribute has a meaning that is not
the same as the "id", and both attributes may be present with
different values.

On a form element, if both "name" and "id" attributes are present,
their values must be the same.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 14 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.