• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

w3c validation insanity

Question

DrKrepz    2

OK so I've been working on this site for a university project and i've come to validate the XHTML and found this: http://validator.w3.org/check?uri=http%3A%2F%2Fjusebox.tk%2Fbrowse.php&charset=%28detect+automatically%29&doctype=Inline&group=0&user-agent=W3C_Validator%2F1.2

Most of (if not all) of these errors are due to either characters in URLs which occur in javascript, or characters which occur in RSS feeds, which I am echoing out via PHP. I have absolutely no idea how to tackle this issue and get this to validate. Can anyone help me out?

the page is at http://jusebox.tk/browse.php

Cheers :)

Share this post


Link to post
Share on other sites

12 answers to this question

Recommended Posts

  • 0
ZakO    489

Html encode your ampersands & -> & if you're outputting using PHP use htmlspecialchars or htmlentities

Share this post


Link to post
Share on other sites
  • 0
DrKrepz    2

thats the problem. if i do that to the ampersands in the sharing urls (which i am defining in js), then the share links don't work.

Share this post


Link to post
Share on other sites
  • 0
+DonC    607

I'd recommend one of two things:

1. Put your javascript in a separate .js file and load it. This gets around all possible problems of the kind you're having.

or

2. Use the CDATA feature, e.g.

<script type="text/javascript">

<![CDATA[

// Your javascript code

]]></script>

Share this post


Link to post
Share on other sites
  • 0
DrKrepz    2

this si how my js works:

&lt;script type="text/javascript"&gt;
		var last_url;

		$(document).ready(function(){
		   manage_goto();
		});

		function manage_goto(){
			$('#go_to').click(function(){
			  last_url =  $('#url').val();
			  load_page();
			});
			$('.all_feeds a').click(function(){
			   last_url = $(this).attr('href');
			   load_page();
			});
		}

		function load_page(){

			update_fb_link();

		}


		function update_fb_link(){
			var share_url = 'http://www.facebook.com/sharer.php?t=Sharing&amp;u='+last_url+'&amp;title=via+Jusebox';
			$('#fb_link').attr('href', share_url);			
		}

I've tried changing the ampersands but as I said it renders the links useless.

Also, I don't actually know how to write php lol. I have a friend who helped me out with it but I can;t get through to him at the moment. I read those links you sent me and although I understand how it works, I have know idea how to implement it. This is my code:

&lt;?php
					$doc = new DOMDocument();
					$doc-&gt;load("http://feeds.eonline.com/eonline/uk/topstories?format=xml");
					$arrFeeds = array();
					foreach ($doc-&gt;getElementsByTagName('item') as $node) {
						$itemRSS = array ( 
							'title' =&gt; $node-&gt;getElementsByTagName('title')-&gt;item(0)-&gt;nodeValue,
							'link' =&gt; $node-&gt;getElementsByTagName('link')-&gt;item(0)-&gt;nodeValue,
							'description' =&gt; strip_tags($node-&gt;getElementsByTagName('description')-&gt;item(0)-&gt;nodeValue),
							);
						array_push($arrFeeds, $itemRSS);
					}

					foreach ($arrFeeds as $value) {
						echo "&lt;a class='rss_link' target='frame' href='".$value[link]."'&gt;".$value[title]."&lt;/a&gt;";
						echo	substr("&lt;p class='rss_description'&gt;".$value[description], 0, 200).'...&lt;/p&gt;';
					}

					?&gt;

any idea how i should go about this? Anything anyone can come up with is really appreciated! My deadline is nearing lol :s

I'd recommend one of two things:

1. Put your javascript in a separate .js file and load it. This gets around all possible problems of the kind you're having.

or

2. Use the CDATA feature, e.g.

<script type="text/javascript">

<![CDATA[

// Your javascript code

]]></script>

thats a really good idea, having the js in a separate file. I'll try that now. thanks :)

Share this post


Link to post
Share on other sites
  • 0
+DonC    607

OK. So I think putting the javascript code inside a CDATA section is the thing to do.

Share this post


Link to post
Share on other sites
  • 0
DrKrepz    2

really? I just created separate js files for the offending script and it works like a charm.

just out of interest (i'm learning, lol) would it be better practice to use the CDATA tags and if so why?

Also, is there anything you could suggest to treat my php problem? thanks :blush:

Share this post


Link to post
Share on other sites
  • 0
+DonC    607

Either method will work. I just don't know enough about PHP to know which rules apply where to files, so figured that CDATA would be safest to do.

Glad to hear it's sorted! :)

Share this post


Link to post
Share on other sites
  • 0
DrKrepz    2

Ah ok. Yeah, the PHP doesnt relate to the JS at all... theyre two separate issues. Thanks a lot for the advice! you solved half my problem :)

Anyone else have any ideas about the PHP? I'm going a little bit insane :blink:

Share this post


Link to post
Share on other sites
  • 0
sweetsam    31

You could do the htmlspecialchars and when you use that in js change & back to & before using it.

Share this post


Link to post
Share on other sites
  • 0
DrKrepz    2

I'm not sure I understand what you mean. Do you think you could elaborate? I really know very very little about PHP, which is why I'm having so much trouble with it lol.

Share this post


Link to post
Share on other sites
  • 0
sweetsam    31

What I meant to say is when you print links use htmlspecialchars function to encode special characters like & so that it validates with W3C. When you are using this data in JavaScript perform a global replace of & with & and then proceed. That way the scripts will work and the page will validate.

Share this post


Link to post
Share on other sites
  • 0
DrKrepz    2

Do you think you could give me an example of how to implement the htmlspecialchars function into my php? As I said I don't really understand it :s

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.