Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Getting Started / July 2007



Tip: Looking for answers? Try searching our database.

JavaScript question for document.domain

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AppDeveloper - 21 Jul 2007 01:59 GMT
Hi,

How come whenever I use "document.domain" in my JavaScript, the alert doesn't
work anymore?

I have the following code below that
function populateMessage() {
    <CFIF SetDomain IS "Y">
      document.domain = "#CGI.Server_Name#";
    </CFIF>

}
BKBK - 21 Jul 2007 20:23 GMT
It works. Just remove the quotes from the snippet [i]..._Name#"...[/i].
AppDeveloper - 21 Jul 2007 23:53 GMT
BKBK,

Sorry, I had a typo, the quotes were not supposed to be in the snippet
"<cfoutput>#CGI.Server_Name#"</cfoutput>" in the first place.  The original
snippet is "<cfoutput>#CGI.Server_Name#</cfoutput>" instead.  I have the script
executed in a popup modal dialog window.

The script goes like this:

function showMessage() {
document.domain = "<cfoutput>#CGI.Server_Name#</cfoutput>";
   with (document.formName) {
           alert("Message here");
           //This next line works without an alert
          fieldName.focus();
    }
}

If I run the script in this domain "http://somedomain/services/index.cfm", I
don't need the "document.domain" line and the alert works.

If I run it in "http://travel.companyname.com/services/index.cfm", I need to
have "document.domain" there for security reason, but the alert won't work.

Thanks!
BKBK - 22 Jul 2007 16:12 GMT
I get no problems at all with the following code. It displays three alerts,
[i]Message here[/i], [i]127.0.0.1[/i]  and [i]Show message[/i].

<html>
<head><title>Document.domain test</title>
<script type="text/javascript">
    function showMessage() {
        document.domain = "<cfoutput>#CGI.Server_Name#</cfoutput>";
        with (document.formName) {
            alert("Message here");
            alert(document.domain);
            alert(btn.value);
        }
    }
</script>
</head>
<body>
<cfform name="formName">
    <cfinput name="btn" type="button" value="Show message"
onclick="showMessage()">
</cfform>
</body>
</html>
AppDeveloper - 22 Jul 2007 21:12 GMT
BKBK,

Maybe I have something in my page that's causing the alert not to work. I'll take a look at it again.

Thanks for all you help!
AppDeveloper - 23 Jul 2007 20:51 GMT
There was something wrong with my script, and that's why the alerts were not working.

Anyway, I've fixed it.

Thanks for your help again!
BKBK - 24 Jul 2007 06:03 GMT
[i]There was something wrong with my script[/i]
Please do tell.
AppDeveloper - 27 Jul 2007 21:20 GMT
Ok sure, here's my telling...

<CFIF CGI.Server_Name CONTAINS "companyname.com">
    <CFSET SetDomain = "Y">
    <CFSET SubDomain = Find(".", CGI.Server_Name, 1)>
    <CFSET DocDomain = Mid(CGI.Server_Name, SubDomain + 1, Len(CGI.Server_Name))>   
</CFIF>

<head>
<CFOUTPUT>
<SCRIPT LANGUAGE="JavaScript">
<!--
function populateData() { //This function is executed onload of page
    <CFIF SetDomain IS "Y">
        document.domain = "#CGI.Server_Name#"; //Output is "travel.companyname.com"
    </CFIF>
                     //Passing large amount of text (over a few thousand
characters) into this modal dialog from parent (that opens this dialog), as
passing large amount of data thru URL parameters from parent will crash
    var parentWindow = dialogArguments;
    var bigContent = parentWindow.document.formName.TextContent.value;
    document.all.id_Content.innerHTML = bigContent;

    <CFIF SetDomain IS "Y">
        //Need to set domain back to "companyname.com".
        document.domain = "#DocDomain#"; //Output is "companyname.com"
    </CFIF>
   
    //Alerts in the scripts following won't work if I don't set document.domain
back to "companyname.com".
}

function customFunction_1() {
    //Alerts in this function won't work if I don't set document.domain back to
"companyname.com" in populateData().
}

function customFunction_2() {
    //Alerts in this function won't work if I don't set document.domain back to
"companyname.com" in populateData().
}
// -->
</SCRIPT>
</CFOUTPUT>
</head>

<body onload="populateData() ">
<table>
<tr>
<td>Display Data from Parent Page:</td>
<td id="id_Content"></td>
</tr>
</table>
<!--- customFunction_1()  and  customFunction_2() are executed somewhere below
this table; i'm not going to list it all. --->
</body>
BKBK - 28 Jul 2007 09:10 GMT
It seems like your fix avoided the problem rather than solve it. Two
suggestions:

1) <SCRIPT LANGUAGE="JavaScript"> is deprecated in current HTML. Use <SCRIPT
TYPE="text/javascript"> instead. However, that is not the cause of the problem.
What follows is.

2)document.domain is a standard Javascript variable to which the Javascript
engine automatically assigns a default value. To see that, open a page
containing just this code:

<script type="text/javascript">
alert(document.domain);
</script>

What you've been doing is forcing document.domain to take [i]your[/i]  value.
What then happens is that it always reverts to the value assigned by the engine
at the earliest opportunity in the code. There are perhaps occasions when that
value is not  "companyname.com".

The solution is simple, and it won't require you to change your script. Just
replace [i]document.domain[/i]  by your own custom variable name, for example,
[i]myDomain[/i].
AppDeveloper - 29 Jul 2007 23:24 GMT
Ok sure, I'll implement your suggestions.

Thanks.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.