1
votes

I have inherited a external page where I have no control:
I have javascript sorting on that page: like http://www.exampledomain.com/javascript:void(1);
Now it has many links like this, the 1 you see is dynamic, what I want to is: convert this code to ColdFusion URL like http://www.exampledomain.com/sor=1&sort=asc & desc. The 1 should work as it is, like it should keep its value as it is 1,2,3,4 etc. I tried to do this with jQuery.

How can I alter these links in ColdFusion?

I tried to come up with some of Javascript solution but it did not work

$('#container').find('a').attr('href', function(i, old) { 
     var col = decodeURIComponent(old).match(/javascript:\s*sort\((.*?)\)/)[1];
return hrefcall+data+'&sortBy='+col;

Thanks

1

1 Answers

0
votes

Your question is unclear, the source of the data is unknown and there are a few typoes.

This JQuery (1.9.1)

var ihref = "";
var col = 'somedata';

$("a").click(function (i) {
    ihref = $(this).attr('href');
    if (ihref.match(/javascript:\s*sort\(\d+\).*/i)) {
        ihref = ihref.replace(/javascript:\s*sort\((\d+)\).*/i,"http://www.w3schools.com/sor=$1&sortBy=" + col);
        $(this).attr('href',ihref);
        alert("As a demonstration, you\'ll see the link is rewritten when a javascript:sort url is clicked."); 
    } 
});

Will convert all javascript:void links as you wanted. I did change the dummy url to w3schools.com because the good folks who own w3 permit their site to be loaded in Iframes which is necessary to easily demonstrate that this code works.

Of course, JQuery only works when JS is enabled. Still, since you started with JQuery, I thought I might show you a working demonstration.

(The links don't actually work, because w3schools doesn't have pages at those points, but you can see in the status bar, the links are rewritten).


If you're retrieving the page via cfhttp.filecontent, you can do something like this

<cfset cfhttp.filecontent = ReReplaceNoCase(cfhttp.filecontent,"javascript:\s*sort\((\d+)\);?","http://www.w3schools.com/sor=\1&sort=asc","ALL")>

The ReReplaceNoCase() was tested against this sample code..

<cfset cfhttp = {} />
<cfset col = "somedata" />
<cfsavecontent variable="cfhttp.filecontent">
  <a href="javascript:void(0);">test - will not alter url</a><Br />
  <a href="javascript:sort(53)">test - will alter url</a><Br />
  <a href="javascript: sort(53)">test - will alter url</a><Br />
  <a href="http://www.w3schools.com">test - will not alter url</a><Br />
</cfsavecontent>
<cfset cfhttp.filecontent = ReReplaceNoCase(cfhttp.filecontent,"javascript:\s*sort\((\d+)\);?","http://www.w3schools.com/sor=\1&sortBy=#col#","ALL")>
<cfdump var="#cfhttp#">