Related to my question here.
I’ve got a JavaScript statement as follows:
<script type="text/javascript">
var u1 = '%pu1=!;';
if (u1.search('NBSLoan|Accept') > -1)
{
var pvnPixel = '<img src="someurl.com"/>';
document.writeln(pvnPixel);
}
if (u1.indexOf('NBSLoan|Refer') > -1)
{
var pvnPixel2 = '<img src="someurl2.com"/>';
document.writeln(pvnPixel2);
}
if (u1.indexOf('DeBSLoan|Accept') > -1) {
var pvnPixel3 = '<img src="someurl3.com"/>';
document.writeln(pvnPixel3);
}
</script>
Given that the u1 variable is a macro that may contain any of the following values:
NBSLoan|Accept|PPI+No|48Months
NBSLoan|Refer|PPI+No|48Months
NBSLoan|Accept|PPI+No|48Months
NBSLoan|Refer|PPI+No|48Months
Also bear in mind the last part (48 months) may change, how can I write a JavaScript statement that will only check the first part of the string? i.e. whether it is “NBSLoan|Accept” etc?
If you put the above statement in a JSFiddle, yes it does work but I assure you (in live) this isn’t working so I’m exploring other possibilities of writing this statement. Could I do something like:
if (u1.search(/NBSLoan|Accept/)? Would this even work properly with pattern matching? At the moment what is happening is that if u1 is equal to NBSLoan|Accept both url1 and url2 are being fired. What am I doing wrong?
Since your parameters are delimited (seperated) by
|‘s, we can split on the|‘s and check the bits between them, like this:Let me know if that does the trick. Also,
writelnisn’t a standard function ofdocumentso if you haven’t declared it previously somewhere, that would be another issue.