Connecting Tech Pros Worldwide Help | Site Map

javascript headache :(

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 17th, 2006, 12:15 PM
plank@plank.com
Guest
 
Posts: n/a
Default javascript headache :(

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


  #2  
Old November 17th, 2006, 01:35 PM
shimmyshack
Guest
 
Posts: n/a
Default 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');

  #3  
Old November 17th, 2006, 03:45 PM
Daz
Guest
 
Posts: n/a
Default 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.
Quote:
}
  #4  
Old November 17th, 2006, 06:05 PM
Davey
Guest
 
Posts: n/a
Default 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>





  #5  
Old November 18th, 2006, 10:25 AM
plank@plank.com
Guest
 
Posts: n/a
Default 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.
Quote:
>}
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!

  #6  
Old November 18th, 2006, 10:55 AM
Daz
Guest
 
Posts: n/a
Default 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! :)

  #7  
Old November 19th, 2006, 06:15 AM
shimmyshack
Guest
 
Posts: n/a
Default 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

  #8  
Old November 19th, 2006, 12:25 PM
Jonas Raoni
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

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 220,989 network members.