Having Ajax links combined with normal page request

I’m trying to find out if it’s possible to have ajax links together with normal Post request.

Eg. I have a link on my website, when users click on this link and have JavaScript enable it should work together with ajax and load the page using Ajax. Otherwise if the user has JavaScript disabled the page should load normally. How do I setup my link to be able to do this?

Is this possible?

Any help would be highly appreciated

This is how I do this kind of thing.

I make the link like I normally would, with the href set to the "fail safe" page. I then create an onclick javascript event for that anchor tag to do the javascript functionality and then return false. So something like this:




<a href="/site/editPost" onclick="doEditPost(); return false;">Edit Post</a>



If you are using something like jQuery (since it comes with Yii), you can also do the following:




<a id="editPost" href="/site/editPost">Edit Post</a>


<script type="text/javascript">

  $(document).ready(function(){

    $('#editPost').click(function(){

      doEditPost();

      return false;

    });

  });

</script>



Is that what you are trying to accomplish?

Another thing worth mentioning is the case when people disallow ActiveX in IE. By default jQuery will respond but ajax will not work because Microsoft.XMLHTTP isn’t allowed to load. (Flash content is also affected by disallowing ActiveX, but that’s a different story.)

From jquery.js (1.3.2)

Since IE6 seems to be more or less history it might be worth to give XMLHttpRequest a try in IE7/IE8. Seems to work for me, so far.




  $cs = Yii::app()->clientScript;

  $cs->registerScript( 'no-activex', 'jQuery.ajaxSetup({ xhr:function(){ return new XMLHttpRequest();} })');

  ...



/Tommy

I’m trying to accomplish exactly what you are explaining, I just want to do this the Yii way

I would think it would be something like this. And I only know this because I spent a bunch of time a couple weeks ago trying to figure it out. Except that I just found a new option for it :D




 echo CHtml::ajaxLink(

	'Update Post', // The text for the anchor tag

	'/site/updatePost', // The url for the ajax request

	array( // The ajaxOptions (jQuery stuff)

		'dataType' => 'json', // Page will output json to parse

		'success' => 'js:handeSuccess', // javascript function to call on success

		'data' => array('ajax' => 1, ...), // The $_GET data (parameters) to pass

	),

	array( // The htmlOptions for the anchor tag

		'href' => '/site/updatePost' // This is what the href of the anchor will be, defaults to #

		// ^^ that's the new option I found out about, I didn't know it worked that way...

	)

);



By passing ‘ajax’ in, you can then just put a switch at the bottom of your action that is something like this:




// If ajax request, output the json result

if (isset($_REQUEST['ajax']) && $_REQUEST['ajax'])

{

	echo json_encode($result);

	Yii::app()->end(); // Not necessarily required, but it can't hurt

}

// Otherwise, render the normal HTML template

else

{

	$this->render('updatePost', array(

		'result' => $result,

	));

}



I think that should basically do the same thing that I wrote above but in an automated way…

This solved my problem, thank you so much. It now loads when JavaScript is enabled and also loads when not. The other thing is it’s more search engine friendly I think.

This is correct. It allows the search engines to also spider and index the pages that would otherwise only be accessible via ajax. The usefulness of that, however, only depends on whether or not the pages need to be indexed and if their content is relevant.

Thanks, SEO is important for this site. Anyway thanks for the help!!!!