/    Sign up×
Community /Pin to ProfileBookmark

AJAX problems

Hey all. I’ve been running into a ton of problems lately because I’m developing a site that’s completely AJAX-driven. For example, instead of linking from mydomain.com/page1.html to mydomain.com/page.html, it instead does mydomain.com/#!/page1.html to mydomain.com/#!/page2.html. So here are the problems I’m having:

Problem 1: dynamically loading css and js.

Doing something like

[code=html]$(‘#mydiv’).load(‘page2.html’)[/code]

is great and all. But it doesn’t load in page2.html’s js and css. This is indeed fixable by using simple javascript such as

[code=html]
function loadjscssfile(filename, filetype){
var fileref=document.createElement(‘script’)
fileref.setAttribute(“type”,”text/javascript”)
fileref.setAttribute(“src”, filename)
}
[/code]

which I would run when clicking the link for page2 like so:

[code=html]
<a href=”#!/page2.html” onclick=”loadjscssfile(‘http://mydomain.com/page2.js’, ‘js’);”>link</a>
[/code]

however, the problem arises if I were to give my friend the link [url]http://mydomain.com/#!/page2.html[/url]. It would load the page just fine, but it doesn’t know what javascript files to load.

Problem 2: delegating handlers to future, not-currently-existing elements.

Let’s say I want to use a jQuery date picker. It’s usually quite simple, because I would just have something like this:

[code=html]
$(function() {
$(“#datepicker”).datepicker({dateFormat: ‘yy-mm-dd’, changeMonth: true, changeYear: true});
});
[/code]

however, #datepicker doesn’t exist until page2.html loads. The only thing I’ve been able to get working is:

[code=html]
$(‘#datepicker’).live(‘click’, function() { $(“#datepicker”).datepicker({dateFormat: ‘yy-mm-dd’, changeMonth: true, changeYear: true});
});
[/code]

but as I understand it, .live has been deprecated as of a few years ago. Now the recommended function is .on or .delegate. However, none of these has worked out for me. According to the documentation, the above is supposed to be the same as

[code=html]
$(document).delegate(“#datepicker”, “click”, function(){ $(“#datepicker”).datepicker({dateFormat: ‘yy-mm-dd’, changeMonth: true, changeYear: true}); }); // jQuery 1.4.3+
[/code]

or

[code=html]
$(document).on(“click”, “#datepicker”, function(){ $(“#datepicker”).datepicker({dateFormat: ‘yy-mm-dd’, changeMonth: true, changeYear: true}); }); // jQuery 1.7+
[/code]

but as of yet I haven’t been able to get any of those 2 to work. Any sort of advice to any of these questions would be greatly appreciated.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@GalwayauthorNov 29.2011 — I've fixed my problem for problem 2. I just had an old version of jQuery. However my problem now is this:

[code=html]
$(document).on("click", "#datepicker", function() { $("#datepicker").datepicker({dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true});
$("#datepicker2").datepicker({dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true}); });
[/code]


my problem is here: $(document).on([U]"click"[/U], "#datepicker", function() {

I need that to be like load or something because right now i have to click it to execute it. then click it again to see the calendar. any thoughts of what the event could be (load doesn't work).
Copy linkTweet thisAlerts:
@BelrickNov 29.2011 — Well

Your using ajax which means server code.

All my dynamic loading is done from the server since the server via sql knows what the end user needs to see.

eg:

(loads a default skin first then replaces elements with customer specific ones.)
[code=html]

<link rel='stylesheet' type='text/css' href='index.css' />
<?php
echo "<link rel='stylesheet' type='text/css' href='skins/".$_SESSION['accountname']."/index.css' />";
?>
[/code]
Copy linkTweet thisAlerts:
@GalwayauthorNov 29.2011 — Well
[code=html]

<link rel='stylesheet' type='text/css' href='index.css' />
<?php
echo "<link rel='stylesheet' type='text/css' href='skins/".$_SESSION['accountname']."/index.css' />";
?>
[/code]
[/QUOTE]


What page exactly does this even go on?

If I've gone to mydomain.com, everything from here is strictly ajax calls. To my understanding, PHP can only be read once, before the page is loaded. If I were then to click a link which essentially does a $("body").load('page2.php'), none of the css from that file is loaded, or this is the part I have wrong?
Copy linkTweet thisAlerts:
@GalwayauthorNov 29.2011 — I did a little test and it's pretty interesting.

The CSS only loads if the page that your doing the .load() to you did

[code=html]
<style>
#content {
background-color:#0F0;
}
</style>
[/code]


as opposed to

[code=html]
<link href="page2.css" rel="stylesheet" type="text/css"/>
[/code]


apparently you can't use external references.
Copy linkTweet thisAlerts:
@BelrickNov 29.2011 — Sorry i dont do jquery stuff.

Never found a need.

The php echo goes in the <head> section and i use it for skins after the person logs in.
Copy linkTweet thisAlerts:
@GalwayauthorNov 29.2011 — I did a little test and it's pretty interesting.

The CSS only loads if the page that your doing the .load() to you did

[code=html]
<style>
#content {
background-color:#0F0;
}
</style>
[/code]


as opposed to

[code=html]
<link href="page2.css" rel="stylesheet" type="text/css"/>
[/code]


apparently you can't use external references.[/QUOTE]


Nevermind, this is not actually true.
×

Success!

Help @Galway spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...