ColdFusion中Javascript代码的URL替换

时间:2014-12-15 00:34:16

标签: coldfusion

我继承了一个我无法控制的外部页面:
我在该页面上进行了javascript排序:例如http://www.exampledomain.com/javascript:void(1);
现在它有很多像这样的链接,你看到1是动态的,我想要的是:将此代码转换为像http://www.exampledomain.com/sor=1&sort=asc & desc这样的ColdFusion URL。 1应该按原样工作,就像它应该保持其值1,2,3,4等。我试图用jQuery做到这一点。

如何在ColdFusion中更改这些链接?

我试图提出一些Javascript解决方案,但它无法正常工作

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

谢谢

1 个答案:

答案 0 :(得分:0)

你的问题不清楚,数据来源不明,还有一些错字。

这个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."); 
    } 
});

将根据需要转换所有javascript:void链接。我确实将虚拟网址更改为w3schools.com,因为拥有w3的优秀人员允许他们的网站加载到iframe中,这对于轻松证明此代码有效是必要的。

当然,JQuery仅在启用JS时才有效。不过,既然你开始使用JQuery,我想我可能会给你一个working demonstration

(这些链接实际上并不起作用,因为w3schools在这些点没有页面,但你可以在状态栏中看到,链接被重写了。)


如果您是通过cfhttp.filecontent检索页面,则可以执行以下操作

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

ReReplaceNoCase()针对此示例代码进行了测试..

<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#">