• 0

Javascript: Object Exists?


Question

keitheronie

Hey all.

I have a javascript file that I created that creates Modal Pop ups within a browser. As I went with it, it got more and more advanced, but nonetheless I've run into a minor snag.

Basically, the script initiates a certain layer--if it does not exist, it will create the pop up with the set parameters in the function. However, if it does exist, it won't create it, it will just show the pop up and the whole nine yards.

My problem is with determining if the pop up--a DIV--exists already in FireFox. The script seems to function as I want it to in IE, but not in FireFox so I know I must be doing something majorly wrong for that to happen.

So my question is, in FireFox, how can you tell if an object exists? In this case, a layer?

Thanks for your help.

Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0
keitheronie

Well, I figured out a way to do it for now, but its much more than I wanted to use.

	//Check if element is already made--if not, create
	for(var i = 0; i < document.getElementsByTagName('DIV').length; i++)
	{
		//Check to see if its the one we're looking for
		if(document.getElementsByTagName('DIV')[i].id == rootname)
			activeModal == document.getElementById(rootname);
	}
	if(activeModal == null)
		modalCreatePopup(type, image, rootname, title, content, width, buttons);

I was hoping to use something simple like

if(document.getElementById(rootname) == null)
	modalCreatePopup(type, image, rootname, title, content, width, buttons);

But like I said before, that didn't seem to work in firefox. It wouldn't create the popup.

If anyone has a simpler code that seems to work, please let me know, it'd be greatly appreciated.

Link to post
Share on other sites
  • 0
jwmcpeak

That's odd. I just wrote up a quick test case and it works fine for me.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
	<title>Non-Existent </title>
	<script type="text/javascript">

	function window_load() {
		if (document.getElementById("div1") != null)
			alert("div1 exists");

		if (document.getElementById("div2") == null)
			alert("div2 doesn't exist");
	}

	onload = window_load;
	</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>

Got a link?

Link to post
Share on other sites
  • 0
+mrbester
.
.if(document.getElementsByTagName('DIV')[i].id == rootname)
			activeModal == document.getElementById(rootname);

Procedural error: == is equivalence check so you have an unused comparison rather than an assignment:

if(document.getElementsByTagName('DIV')[i].id == rootname)
  activeModal = document.getElementById(rootname);

However, shorter code would work just as well:

var activeModal = document.getElementById(rootname) || modalCreatePopup(type, image, rootname, title, content, width, buttons);

as the element either exists or it doesn't.

Link to post
Share on other sites
  • 0
keitheronie

Thank you. I ended up rewriting the entire script last night (before reading the replies) and it works great.

Thanks guys!

Link to post
Share on other sites
This topic is now closed to further replies.
  • Recently Browsing   0 members

    No registered users viewing this page.