Welcome to WebmasterWorld Guest from 3.214.184.124

Forum Moderators: open

Message Too Old, No Replies

using jQuery.get and $(#idReference).append in header

help with XHTML 1.0 Strict validation

     
3:15 am on Nov 14, 2008 (gmt 0)

New User

10+ Year Member

joined:Jan 19, 2008
posts: 40
votes: 0


Hello :)

Ive recently been working on getting my site valid XHTML 1.0 Strict, and most of it is, except 2 parts, which is some javascript issues in the HTML header.

My javascript library of choice is jQuery. (just to point that out about the javascript commands lol)

heres problem #1:

Im using PHP to generate a 'where you are currently at, and how you got there' navigation based on how deep the viewer is in the site. once thats generated, i use javascript to write it out into a javascript generated area of the website. All of this javascript generating/writing is done in the <head> section. the problem is that the validator is seeing the <a></a> tags and throwing an error, even though its not actually being written in the head. how can i get this to validate?

Example:


PHP:

$linkURL = 'http://www.example.com/index.php';
$linkArea = '?area='.$Area;
$linkLevel1 = '&amp;level1='.$Level1;
$linkLevel2 = '&amp;level2='.$Level2;
$linkLevel3 = '&amp;level3='.$Level3;
$linkLevel4 = '&amp;level4='.$Level4;
$navHome = '<a href="'.$linkURL.'">Home</a>';
$navArea = '<a href="'.$linkURL.$linkArea.'">'.$Area.'</a>';
$navLevel1 = '<a href="'.$linkURL.$linkArea.'&amp;sub=1'.$linkLevel1.'">'.$Level1.'</a>';
$navLevel2 = '<a href="'.$linkURL.$linkArea.'&amp;sub=2'.$linkLevel1.$linkLevel2.'">'.$Level2.'</a>';
$navLevel3 = '<a href="'.$linkURL.$linkArea.'&amp;sub=3'.$linkLevel1.$linkLevel2.$linkLevel3.'">'.$Level3.'</a>';
$navLevel4 = '<a href="'.$linkURL.$linkArea.'&amp;sub=4'.$linkLevel1.$linkLevel2.$linkLevel3.$linkLevel4.'">'.$Level4.'</a>';
$navDivider = ' >> ';
$NAVDONE = '';
if ($Sub >= '0' !isset($Sub) $Sub == '') {
$NAVDONE .= $navHome.$navDivider.$navArea;
}
if ($Sub >= '1') {
$NAVDONE .= $navDivider.$navLevel1;
}
if ($Sub >= '2') {
$NAVDONE .= $navDivider.$navLevel2;
}
if ($Sub >= '3') {
$NAVDONE .= $navDivider.$navLevel3;
}
if ($Sub >= '4') {
$NAVDONE .= $navDivider.$navLevel4;
}
if ($Area == 'Home' $Area == '' !isset($Area)) {
$NAVDONE = $navHome;
}

Javascript:
jQuery(document).ready(function() {
jQuery('a[rel*=facebox]').facebox()
$('#facebox .navHeader').append('<?PHP echo $NAVDONE; ?>')
})

It complains about the <a></a> tags in the header.

problem #2:


jQuery.facebox(function($) {
jQuery.get('<?PHP echo 'example.php?area='.urlencode($_REQUEST['area']).'&sub='.urlencode($_REQUEST['sub']).'\&level1='.urlencode($_REQUEST['level1']).'\&level2='.urlencode($_REQUEST['level2']).'\&level3='.urlencode($_REQUEST['level3']).'\&level4='.urlencode($_REQUEST['level4']);?>', function(data) { jQuery.facebox(data) })
})

The PHP in this snippet just adds the correct variables to the url. My problem is the ampersands ("&").
I cant just use "&amp;" because Im feeding it to the browser using jQuery.get, then sending the data to another javascript generated area of the page. Help?

[edit] removed personal links

3:23 am on Nov 14, 2008 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:5021
votes: 26


1. You need to escape the / character. So </a> becomes <\/a>.

2. Regardless, you should still be escaping the ampersand as &amp;. jQuery should be able to take a string like "example.php?area=x&amp;sub=y". If it can't, then I would call that a pretty big jQuery bug.

Hope that helps.

3:31 am on Nov 14, 2008 (gmt 0)

New User

10+ Year Member

joined:Jan 19, 2008
posts:40
votes: 0


if i escape the / character (<\/a>) it throws error an error stating that i didnt close the tag.

[edited by: eelixduppy at 12:21 pm (utc) on Nov. 14, 2008]
[edit reason] disabled smileys [/edit]

3:35 am on Nov 14, 2008 (gmt 0)

New User

10+ Year Member

joined:Jan 19, 2008
posts: 40
votes: 0


oh, and if i use the &amp; instead of & jQuery gets the following url:
http://www.example.com/SWR/chaos.php?area=Catalog&amp;sub=3&amp;level1=Ribbons+%2F+Miscellaneous&amp;level2=Drapes&amp;level3=Pin+Ribbon+Drapes&amp;level4=DIA001

It isnt converting the &amp; to &

8:17 am on Nov 14, 2008 (gmt 0)

New User

10+ Year Member

joined:Jan 19, 2008
posts: 40
votes: 0


meh, i solved it by using functions in an external script.

For the first, i used PHP's function 'htmlentities' to encode the html characters, then sent them to the external script, then decoded them using another javascript function, 'html_entity_decode' (check out the php.js project). after having successfully (and validly) got the url from the main page to the external script, i then launched the function i had been previously using.

for the second, i sent the specific variables via another javascript function, then put the url together in the external file, then sent it to the browser using jQuery.get();

(I really didn't want to do this, it was just more work than id hoped for. im not too big on JS, but its necessary lol)

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members