We've been asked to put in a band-aid measure to some code so this is going to look really dumb but bear with me.
We need to prevent an onclick method of an anchor and retrieve the parameters of the function call being made in order to call a different function with those parameters. Here is the code and html we have so far:
<p><a href="#" onclick="openMoreProductVideos('Title of video','VideoCode','false');return false;"><span class="txt">View all videos</span></a></p>
JS:
// undefine onclick so it is not handled with broken function
$('a[onClick^=openMoreProductVideos]').each(function () {
this.onclick = undefined;
});
// grab parameters from onclick function call and submit to new function.
$('a[onClick^=openMoreProductVideos]').click(function () {
strStart = "openMoreProductVideos(";
strFinish = ");return false"
srctext = $(this).parent().html();
var re = strStart + ".*?"+strFinish
var newtext = srctext.match(re)[1];
var callparams = newtext.substring(1,newtext.length-1);
testparams(callparams);
});
// test function to see if parameters are working
function testparams (a,b,c){
console.log (a,b,c)
}
Problem is that the returned string (callparams) is seen as one parameter. I am not a regex expert so I'm hoping that someone with more experience than I can quickly see the solution.
Many thanks.
if callparams
is a string like "'Title of video','VideoCode','false'"
then try
testparams(callparams.split(','));
/* transforming string into an array
* ["'Title of video'", "'VideoCode'", "'false'"]
*/
...
function testparams (pars){
console.log (pars[0], pars[1], pars[2]);
/* if necessary remove trailing quotes with .replace() method */
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.