Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Script in AJAX-loaded page inside parent page not firing

Reply
Thread Tools

Script in AJAX-loaded page inside parent page not firing

 
 
pbd22
Guest
Posts: n/a
 
      09-11-2009

Hi.

Help much appreciated... thanks

I have a page - Page.aspx.

In the page, there is a button that fires an XMLHTTP event:

return NewPage('srchfrm.aspx?q=' + query.value + "&type=" +
type.value,'tabs-1','tabs-2');

The result is that SrchFrm.aspx is loaded in a DIV on Page.aspx.

Now, in the loaded SrchFrm.aspx there is a drop down list
with associated values. I have a window.load script that
assigns onclick functions for each of the <option> elements
on this page.

If I test this script in SrchFrm.aspx (without
the page being loaded into Page.aspx) it works fine. But, when it is
loaded, it doesn't work.

I am guessing I am missing something along the lines of one form being
loaded into another via ajax. Any suggestions?

Also, Page.aspx has a Window.Onload event but, when I try
to add SrchFrm.aspx's script as a function in this Window.Onload
event, it still does not fire.

Here is my script at the head of SrchFrm.aspx for what it is worth:

<script type="text/javascript">
window.onload = function()
{

var mangaSelect=document.getElementById("fcat");

var maxi = mangaSelect.options.length;

for( var i = 0; i < maxi; i++ )
{
var option = mangaSelect.options[i];
option.addEventListener( "click", toggleElem, true );

}

};


function toggleElem(event)
{
alert(event.target.value);
}
</script>

Thanks in advance.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-11-2009
pbd22 wrote:
> I have a page - Page.aspx.
>
> In the page, there is a button that fires an XMLHTTP event:


XMLHTTP _request_

> return NewPage('srchfrm.aspx?q=' + query.value + "&type=" +
> type.value,'tabs-1','tabs-2');


Useless snippet as NewPage() is user-defined and you did not post its code.

> [...]
> I am guessing I am missing something along the lines of one form being
> loaded into another via ajax. Any suggestions?
>
> Also, Page.aspx has a Window.Onload event but, when I try


window.onload, and it's an event *handler* (for the `load' event).

> to add SrchFrm.aspx's script as a function in this Window.Onload
> event, it still does not fire.
>
> Here is my script at the head of SrchFrm.aspx for what it is worth:
>
> <script type="text/javascript">
> window.onload = function()
> {
> [...]
> var option = mangaSelect.options[i];
> option.addEventListener( "click", toggleElem, true );


Use 2*x spaces for indentation, not tabs (at least when posting here).

You have not told where this code is supposed to run; obviously this cannot
work in Internet Explorer, for example (the MSHTML DOM does not implement
W3C DOM Level 2 Events). However, the .aspx suffix suggests ASP .NET which
in turn suggests IE as targeted client, so that might be the cause of your
problem.

Curious why you use the proprietary window.onload property and then use the
standards-compliant addEventListener() method. The proprietary
event-handler attributes have the advantage that, for historical reasons,
they are widely supported (even in MSHTML/IE); the disadvantage is inherent.
The standards-compliant method has obvious advantages (read the Spec); the
disadvantage is that MSHTML/IE does not support it (as the only UA out
there, AFAIK). Make a design choice or use a wrapper method to support both
(google `_addEventListener').

One wonders if you know what you are doing here, and if you have even RTFM
before you coded, let alone posted; passing `true' for the third argument of
addEventListener() makes this a capturing listener (the element receives the
event before all other elements.)

In addition, the `click' event is not universally supported for `option'
elements (IIRC, MSHTML does not support it there). Use the `click' or
(better) `change' event of the `select' element instead.

<http://jibbering.com/faq/#posting>


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
 
 
 
SAM
Guest
Posts: n/a
 
      09-11-2009
Le 9/11/09 7:46 PM, pbd22 a écrit :
> Hi.
>
> Help much appreciated... thanks
>
> I have a page - Page.aspx.
>
> In the page, there is a button that fires an XMLHTTP event:
>
> return NewPage('srchfrm.aspx?q=' + query.value + "&type=" +
> type.value,'tabs-1','tabs-2');
>
> The result is that SrchFrm.aspx is loaded in a DIV on Page.aspx.
>
> Now, in the loaded SrchFrm.aspx there is a drop down list
> with associated values. I have a window.load script that
> assigns onclick functions for each of the <option> elements
> on this page.
>
> If I test this script in SrchFrm.aspx (without
> the page being loaded into Page.aspx) it works fine. But, when it is
> loaded, it doesn't work.
>
> I am guessing I am missing something along the lines of one form being
> loaded into another via ajax. Any suggestions?
>
> Also, Page.aspx has a Window.Onload event but, when I try
> to add SrchFrm.aspx's script as a function in this Window.Onload
> event, it still does not fire.


How do you expect a WINDOW.onload could fire if the file isn't :
1) LOADED
2) IN a WINDOW

Didn't you speak of a *DIV* ? (which isn't a window)
Aren't the data ('SrchFrm') "innered" in the HTML of the div ?
(the file isn't really "loaded", its lines were passed to a variable)

Put the script at the end of the file 'SrchFrm'.

When Ajaxxing it would be better that the "donwloaded" files have no
HEAD nor body or html tags ...
.... as they are already present in the mother : 'Page'.
(remember you'll feed a div, and not display an html page)


> Here is my script at the head of SrchFrm.aspx for what it is worth:


No real interest for the moment.
Except that it could be simpler ?

function toggleElem(what){
alert(what.options[what.selectedIndex].value);
}

var mangaSelect=document.getElementById("fcat");
mangaSelect.onclick="toggleElem(this);"

--
sm
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
dynamic loaded ascx delegate event not firing in parent form CMELLO@tams.com ASP .Net Building Controls 1 09-12-2008 06:20 PM
dynamic loaded datalist ascx event delegate not firing in parent f cindy ASP .Net 0 09-09-2008 04:16 PM
Page load of the parent page called twice when a modal dialog is opened from a button click of the user control on the parent page Samy ASP .Net 2 08-15-2005 03:30 PM
Getting a page to refresh the parent, even when not loaded from the parent? Bill S. Javascript 2 05-08-2004 04:56 AM



Advertisments