<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>Replace href dynamically using Javascript</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=44046190" />
  <subtitle>Replace href dynamically using Javascript</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=44046190</id>
  <updated>2026-06-12T16:03:39Z</updated>
  <dc:date>2026-06-12T16:03:39Z</dc:date>
  <entry>
    <title>RE: Replace href dynamically using Javascript</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=44054469" />
    <author>
      <name>Ahmed bouchriha</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=44054469</id>
    <updated>2014-10-16T14:39:33Z</updated>
    <published>2014-10-16T14:39:33Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Hi jonatan&lt;br&gt;&lt;br&gt;Try to run your script on the allPortletsReady event instead of AUI().ready&lt;br&gt;&lt;br&gt;The code should be :&lt;br&gt;&lt;br&gt;&lt;pre&gt;&lt;code&gt;Liferay.on('allPortletsReady',

function() {
	AUI().all("a").each(function(node) {
		if (node.attr("href").indexOf("showEjercicio") &amp;amp;gt; -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());
			}
		}
	});
});&lt;/code&gt;&lt;/pre&gt;&lt;br&gt;&lt;br&gt;Best Regards&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>Ahmed bouchriha</dc:creator>
    <dc:date>2014-10-16T14:39:33Z</dc:date>
  </entry>
  <entry>
    <title>Replace href dynamically using Javascript</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=44046189" />
    <author>
      <name>Jonatan Oyola</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=44046189</id>
    <updated>2014-10-16T13:10:24Z</updated>
    <published>2014-10-16T13:10:24Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Hello,&lt;br&gt;&lt;br&gt;I want to replace all href dynamically using Javascript when the page is loaded, because I need to use the class &lt;strong&gt;Liferay.PortletURL&lt;/strong&gt; and it has a problem when the session is timeout.  So I thought that I can replace this value with the url.&lt;br&gt;&lt;br&gt;This piece of code doesn't works:&lt;br&gt;&lt;pre&gt;&lt;code&gt;
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") &amp;amp;gt; -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());
				  }
			  }
			}
		);
	}
);
&lt;/code&gt;&lt;/pre&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;But when I run using the console of firebug, the href were replaced without problem.&lt;br&gt;&lt;br&gt;&lt;pre&gt;&lt;code&gt;
AUI().all("a").each
(
	function(node)
	{
	  if(node.attr("href").indexOf("showEjercicio") &amp;amp;gt; -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());
		  }
	  }
	}
);

&lt;/code&gt;&lt;/pre&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;Any idea?  Thanks&lt;br&gt;&lt;br&gt;Jonatan&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>Jonatan Oyola</dc:creator>
    <dc:date>2014-10-16T13:10:24Z</dc:date>
  </entry>
</feed>
