Forums de discussion

Replace href dynamically using Javascript

thumbnail
Jonatan Oyola, modifié il y a 9 années.

Replace href dynamically using Javascript

Regular Member Publications: 193 Date d'inscription: 06/06/07 Publications récentes
Hello,

I want to replace all href dynamically using Javascript when the page is loaded, because I need to use the class Liferay.PortletURL and it has a problem when the session is timeout. So I thought that I can replace this value with the url.

This piece of code doesn't works:

AUI().ready(

	/*
	This function gets loaded when all the HTML, not including the portlets, is
	loaded.
	*/

	function() {

		Liferay.provide(
			window,
			'showEjercicio',
			function(articleId, title) {
				var url = Liferay.PortletURL.createRenderURL();
				url.setWindowState('maximized');
				url.setPortletId("9_WAR_magnapediaportlet");
				url.setParameter("articleId", articleId);
				url.setParameter("title", title);

				return url.toString();
			},
			['liferay-portlet-url']
		);

		
		AUI().all("a").each
		(
			function(node)
			{
			  if(node.attr("href").indexOf("showEjercicio") > -1)
			  {
				  var fstr =  decodeURI(node.attr("href"));
				  fstr = fstr.replace("javascript", "");
				  fstr = fstr.replace("showEjercicio", "");
				  fstr = fstr.replace(":", "");
				  fstr = fstr.replace(";", "");
				  fstr = fstr.replace("(", "");
				  fstr = fstr.replace(")", "");
				  fstr = fstr.replace(/'/g, "");
				  fstr = fstr.replace(/ /g, "");
				  var args = fstr.split(",", 2);
				  var url = window["showEjercicio"].apply(this, args);
				  if(url)
				  {
					  node.attr("href", url.toString());
				  }
			  }
			}
		);
	}
);



But when I run using the console of firebug, the href were replaced without problem.


AUI().all("a").each
(
	function(node)
	{
	  if(node.attr("href").indexOf("showEjercicio") > -1)
	  {
		  var fstr =  decodeURI(node.attr("href"));
		  fstr = fstr.replace("javascript", "");
		  fstr = fstr.replace("showEjercicio", "");
		  fstr = fstr.replace(":", "");
		  fstr = fstr.replace(";", "");
		  fstr = fstr.replace("(", "");
		  fstr = fstr.replace(")", "");
		  fstr = fstr.replace(/'/g, "");
		  fstr = fstr.replace(/ /g, "");
		  var args = fstr.split(",", 2);
		  var url = window["showEjercicio"].apply(this, args);
		  if(url)
		  {
			  node.attr("href", url.toString());
		  }
	  }
	}
);




Any idea? Thanks

Jonatan
thumbnail
Ahmed bouchriha, modifié il y a 9 années.

RE: Replace href dynamically using Javascript

Junior Member Publications: 55 Date d'inscription: 04/05/12 Publications récentes
Hi jonatan

Try to run your script on the allPortletsReady event instead of AUI().ready

The code should be :

Liferay.on('allPortletsReady',

function() {
	AUI().all("a").each(function(node) {
		if (node.attr("href").indexOf("showEjercicio") > -1) {
			var fstr = decodeURI(node.attr("href"));
			fstr = fstr.replace("javascript", "");
			fstr = fstr.replace("showEjercicio", "");
			fstr = fstr.replace(":", "");
			fstr = fstr.replace(";", "");
			fstr = fstr.replace("(", "");
			fstr = fstr.replace(")", "");
			fstr = fstr.replace(/'/g, "");
			fstr = fstr.replace(/ /g, "");
			var args = fstr.split(",", 2);
			var url = window["showEjercicio"].apply(this, args);
			if (url) {
				node.attr("href", url.toString());
			}
		}
	});
});


Best Regards