PDA

View Full Version : calling ajaxpage function without any event handler



Mashael
10-19-2009, 09:34 AM
1) Script Title:
Dynamic Ajax Content

2) Script URL (on DD):
http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm

3) Describe problem:

Hi,

I just tried the pervious code to dynamically change the content of a div with an external web page

it works fine when I call the function ajaxpage with an event handler like



<body dir="rtl" onload="javascript:ajaxpage('Main.aspx', 'D1'); return false;" >

but when I want to call it within the code or within another javascript function it doesn't work !!

what is the problem

this is a part of my code




<%
Dim P As String
P = Trim(Request("p"))
%>


<script type="text/javascript">

function redir(MyD) {

switch (MyD)
{
case '': ajaxpage('Main.aspx.vb', 'D1'); break;
case 'Main': ajaxpage('Main.aspx.vb', 'D1'); break;
case 'WhoWeAre': ajaxpage('WhoWeAre.aspx.vb', 'D1'); break;
case 'Products': ajaxpage('Products.aspx.vb', 'D1'; break;
case 'Services': ajaxpage('Services.aspx.vb', 'D1'); break;
case 'Distributors': ajaxpage('Distributors.aspx.vb', 'D1'); break;
case 'Offers': ajaxpage('Offers.aspx.vb', 'D1') break;
case 'HowToOrder': ajaxpage('HowToOrder.aspx.vb', 'D1');break;
case 'ContactUs': ajaxpage('ContactUs.aspx.vb', 'D1'); break;
default: ajaxpage('ERR.aspx.vb', 'D1');
}
}

</script>


<head runat="server">
<title>My Page</title>
</head>
<body dir="rtl" onload="javascript:redir('<%=P%>');return false; ">



any idea :)

jscheuer1
10-19-2009, 11:08 AM
This type of AJAX import generally doesn't import your javascript in a functional manner, though (as you've observed) javascript events associated with HTML tags generally will fire. You could -

(1) Put the javascript on the 'top' page (the same page that has the ajaxpage function on or linked to it).

OR -

(2) Try using this as the event in the body tag:


<body dir="rtl" onload="ajaxpage(('<%=P%>' || 'Main') + '.aspx.vb', 'D1');">

It will cover all the case statements from your javascript code except for the default one, which I assume is there in case somehow a page name other than none or those you are anticipating gets passed as Trim(Request("p")). If this is at all likely to happen, my first suggestion would be the better method.

Mashael
10-19-2009, 12:15 PM
Thanks a lot John,

The second method worked great :)

But still I want to try the first one, what do you mean by the TOP Page??
Both of the two functions are defined in the same JavaScript block in the Default page before the head tag,

did you mean something else ?

jscheuer1
10-19-2009, 02:54 PM
Without actually seeing your pages, I'm assuming that you have one or more pages (these we will represent as the 'top' page(s)) that can bring in the code from your post via the ajaxpage function. The code from your post then in turn brings in yet another page via the ajaxpage function based upon the get or post value of p.

If I have that right, now you know where the top page is.

Incidentally, this:


<%
Dim P As String
P = Trim(Request("p"))
%>

and this:


<%=P%>

is server side code, asp or something similar. It runs on the server side as the page is served. The rest of the script code is javascript and runs only as parsed by the browser on the client side.

The tricky part is, a script like so:


<script type="text/javascript">

function redir(MyD) {

switch (MyD)
{
case '': ajaxpage('Main.aspx.vb', 'D1'); break;
case 'Main': ajaxpage('Main.aspx.vb', 'D1'); break;
case 'WhoWeAre': ajaxpage('WhoWeAre.aspx.vb', 'D1'); break;
case 'Products': ajaxpage('Products.aspx.vb', 'D1'; break;
case 'Services': ajaxpage('Services.aspx.vb', 'D1'); break;
case 'Distributors': ajaxpage('Distributors.aspx.vb', 'D1'); break;
case 'Offers': ajaxpage('Offers.aspx.vb', 'D1') break;
case 'HowToOrder': ajaxpage('HowToOrder.aspx.vb', 'D1');break;
case 'ContactUs': ajaxpage('ContactUs.aspx.vb', 'D1'); break;
default: ajaxpage('ERR.aspx.vb', 'D1');
}
}

</script>

will not run on either side. If it is run by the server, there is no function ajaxpage() so there would be an error. And, due to the way that the ajaxpage function works, if this code itself is imported via the ajaxpage function (which I'm just guessing that it is) it just won't run or be seen as javascript. But if it was on what I'm calling the top page, it will be accessible to events on imported content.

Now, there are other possibilities as to how your site is organized in this regard, so I may be misunderstanding what's happening. So if you want more help on this:

Please post a link to the page on your site that contains the problematic code so we can check it out.

Mashael
10-20-2009, 08:08 AM
Hi again,

I really appreciate the explanation :); even I think I didn't get it all :)
And my site still in my PC, I’m using Microsoft Visual Web Developer 2008 Express Edition

I have solve the default situation by butting a span inside the div, which will run in case of non of the presented cases was occured

Forgive me for my questions but I have a one last:
I attempted to have a JavaScript drop-down menu which will have a buttons which use the ajaxpage function, Do you think it will work??

jscheuer1
10-20-2009, 08:21 AM
I think I didn't get it all

That may mean that I didn't get it all. Without seeing the live site I can only guess at some of the things I was saying. I tried to indicate that some it was just guessing. If that didn't come across, let me be clearer - I was just guessing at some of it.

As to your menu idea, perhaps. I really cannot be sure because I'm really not sure what you are proposing, though something like that can work if it is done correctly.