javascript headache :( 
November 17th, 2006, 01:15 PM
| | | |
hey.. ok i need help! :(
let me try and describe my situation.. i have a javascript object which dynamically creates a div
element and uses ajax to call a page for the content of the div. Within this content i need to
reference a function which is defined in the javascript object which created the div. have i lost
anyone???
let me try and explain with some code:
function newDiv()
{
var oDiv = document.createElement("div");
oDiv.id = "div1";
document.body.appendChild(oDivMain);
function LoadMe(sURL)
{
myXMLReq = window.XMLHttpRequest ? new XMLHttpRequest() : new
ActiveXObject("Microsoft.XMLHTTP");
myXMLReq.onreadystatechange = URLRetreived;
var sPage = sURL;
var sParams = ""
var iPos = sURL.indexOf("?");
if (iPos -1)
{
sPage = sURL.substr(0, iPos);
sParams = sURL.substr(iPos + 1) + "&";
}
sParams += "winid=" + Me.WinID;
myXMLReq.open('POST', sURL, true);
myXMLReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
myXMLReq.send(null);
}
function URLRetreived()
{
if (myXMLReq.readyState == 4)
{
if (myXMLReq.status == 200)
{
var oMeDiv = document.getElementById("div1");
oMeDiv.innerHTML = myXMLReq.responseText;
}
else
{
document.getElementById("div1").innerHTML = "ERROR";
}
}
}
function testFunction()
{
alert("hello world");
}
}
var obj = new newDiv();
obj.LoadMe("hello.htm);
hello.htm:
<a href="testFunction();">click me</a>
How can i reference the testFunction defined in the object?
I hope that makes sense!
Thanks in advance,
ajb | 
November 17th, 2006, 02:35 PM
| | | | re: javascript headache :(
i would use a slightly different way:
var newDiv =
{
testFunction : function()
{
alert("hello world");
},
testFunction2 : function(strMsg)
{
alert(strMsg);
}
}
then either
newDiv.testFunction();
or
newDiv.testFunction2('the crazy cat slept in the flat'); | 
November 17th, 2006, 04:45 PM
| | | | re: javascript headache :( plank@plank.com wrote: Quote:
hey.. ok i need help! :(
>
let me try and describe my situation.. i have a javascript object which dynamically creates a div
element and uses ajax to call a page for the content of the div. Within this content i need to
reference a function which is defined in the javascript object which created the div. have i lost
anyone???
>
let me try and explain with some code:
>
function newDiv()
{
var oDiv = document.createElement("div");
oDiv.id = "div1";
document.body.appendChild(oDivMain);
>
function LoadMe(sURL)
{
myXMLReq = window.XMLHttpRequest ? new XMLHttpRequest() : new
ActiveXObject("Microsoft.XMLHTTP");
myXMLReq.onreadystatechange = URLRetreived;
>
var sPage = sURL;
var sParams = ""
var iPos = sURL.indexOf("?");
if (iPos -1)
{
sPage = sURL.substr(0, iPos);
sParams = sURL.substr(iPos + 1) + "&";
}
sParams += "winid=" + Me.WinID;
>
myXMLReq.open('POST', sURL, true);
myXMLReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
myXMLReq.send(null);
}
>
function URLRetreived()
{
if (myXMLReq.readyState == 4)
{
if (myXMLReq.status == 200)
{
var oMeDiv = document.getElementById("div1");
oMeDiv.innerHTML = myXMLReq.responseText;
}
else
{
document.getElementById("div1").innerHTML = "ERROR";
}
}
}
>
function testFunction()
{
alert("hello world");
}
}
>
var obj = new newDiv();
obj.LoadMe("hello.htm);
>
>
>
hello.htm:
>
<a href="testFunction();">click me</a>
>
>
>
>
How can i reference the testFunction defined in the object?
>
I hope that makes sense!
>
Thanks in advance,
>
ajb
| This should also work...
function newDiv()
{
this.testFunction = function()
{
alert("hello world");
}
}
var obj = new newDiv();
obj.testFunction;
'this' refers to the object itself, and is in effect, the actual
object. newDiv.testFunction() and using the new object we made
obj.testFunction() (I think), will both translate to
this.testFunction(). so testFunction() will be called from within the
appropriate object.
I hope that helps and makes sense as I think I have confused myself. If
anyone could clarify what I have written, it would be appreciated and
help me a great deal also.
All the best.
Daz. | 
November 17th, 2006, 07:05 PM
| | | | re: javascript headache :( plank@plank.com wrote:
var obj = new newDiv();
obj.LoadMe("hello.htm);
hello.htm:
<a href="testFunction();">click me</a>
How can i reference the testFunction defined in the object?
Maybe like this
<a href="obj.testFunction();">click me</a> | 
November 18th, 2006, 11:25 AM
| | | | re: javascript headache :(
On 17 Nov 2006 09:00:51 -0800, "Daz" <cutenfuzzy@gmail.comwrote: Quote:
>
>plank@plank.com wrote:
> Quote:
>hey.. ok i need help! :(
>>
>let me try and describe my situation.. i have a javascript object which dynamically creates a div
>element and uses ajax to call a page for the content of the div. Within this content i need to
>reference a function which is defined in the javascript object which created the div. have i lost
>anyone???
>>
>let me try and explain with some code:
>>
>function newDiv()
>{
> var oDiv = document.createElement("div");
> oDiv.id = "div1";
> document.body.appendChild(oDivMain);
>>
> function LoadMe(sURL)
> {
> myXMLReq = window.XMLHttpRequest ? new XMLHttpRequest() : new
>ActiveXObject("Microsoft.XMLHTTP");
> myXMLReq.onreadystatechange = URLRetreived;
>>
> var sPage = sURL;
> var sParams = ""
> var iPos = sURL.indexOf("?");
> if (iPos -1)
> {
> sPage = sURL.substr(0, iPos);
> sParams = sURL.substr(iPos + 1) + "&";
> }
> sParams += "winid=" + Me.WinID;
>>
> myXMLReq.open('POST', sURL, true);
> myXMLReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
> myXMLReq.send(null);
> }
>>
> function URLRetreived()
> {
> if (myXMLReq.readyState == 4)
> {
> if (myXMLReq.status == 200)
> {
> var oMeDiv = document.getElementById("div1");
> oMeDiv.innerHTML = myXMLReq.responseText;
> }
> else
> {
> document.getElementById("div1").innerHTML = "ERROR";
> }
> }
> }
>>
> function testFunction()
> {
> alert("hello world");
> }
>}
>>
>var obj = new newDiv();
>obj.LoadMe("hello.htm);
>>
>>
>>
>hello.htm:
>>
><a href="testFunction();">click me</a>
>>
>>
>>
>>
>How can i reference the testFunction defined in the object?
>>
>I hope that makes sense!
>>
>Thanks in advance,
>>
>ajb
| >
>This should also work...
>function newDiv()
>{
this.testFunction = function()
{
alert("hello world");
}
>}
>
>var obj = new newDiv();
>obj.testFunction;
>
>'this' refers to the object itself, and is in effect, the actual
>object. newDiv.testFunction() and using the new object we made
>obj.testFunction() (I think), will both translate to
>this.testFunction(). so testFunction() will be called from within the
>appropriate object.
>
>I hope that helps and makes sense as I think I have confused myself. If
>anyone could clarify what I have written, it would be appreciated and
>help me a great deal also.
>
>All the best.
>
>Daz. | lol.. thats how I felt when I tried to describe it!
Thanks for all the help and suggestions.. I will be trying them today.. wish me luck! | 
November 18th, 2006, 11:55 AM
| | | | re: javascript headache :( plank@plank.com wrote: Quote: |
Thanks for all the help and suggestions.. I will be trying them today.. wish me luck!
| Luck! :) | 
November 19th, 2006, 07:15 AM
| | | | re: javascript headache :(
this notation using encapsulation to avoid clutering the global
namespace, which just means less hassle later, and it looks nicer I
think.
var newDiv =
{
blah1 : function()
{
//....
},
blah2 : function()
{
//...
}
}
or
var newDiv = {};
newDiv.blah1 = function() {};
newDiv.blah2 = function() {}; http://javascripttoolbox.com/bestpractices/#namespace | 
November 19th, 2006, 01:25 PM
| | | | re: javascript headache :(
shimmyshack escreveu: Quote:
this notation using encapsulation to avoid clutering the global
namespace, which just means less hassle later, and it looks nicer I
think.
>
var newDiv =
{
blah1 : function()
{
//....
},
blah2 : function()
{
//...
}
}
>
| Hi,
I've been out for a long time, but I started missing "Pointed Ears" and
got back =/
I personally prefer this way:
new function(){
var newDiv = {a: 1, b: 2};
}
It really encapsulates everything. Using your method there's still the
possibility of overriding the variable in another place. The bad thing
is that it really encapsulates everything, so this can be used just for
code initializations or closures purposes =/
--
Jonas Raoni Soares Silva http://www.jsfromhell.com |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|