将javascript变量解析为iframe src

时间:2013-03-27 23:41:12

标签: javascript variables iframe click

我被要求在网站订单收据页面上部署价值点击跟踪像素。必须将数据动态放入iframe的src中。

我已将正确的数据声明为变量,如下所示:

<script type="text/javascript">
//makes a copy of the total order revenue
var totalPrice = jQuery('.veryImportant.ordertotal div').html();
jQuery('#buttons').append('<p id="totalPrice">'+totalPrice+'</p>');
//removes the £ pound symbol from the duplicated string
var val = jQuery('#totalPrice').html();
jQuery('#totalPrice').html(val.substring(1, val.length));
//required variables
var orderid = jQuery('.ordernumber strong').text(); //order ID number
var revenue = jQuery('#totalPrice').text(); // total order revenue
var quantity = jQuery('#totalQuantity').text(); //quantity per table row/product name
//testing the variables print the correct values
alert(orderid + " " + revenue + " " + sku + " " + quantity);
</script>

(对于任何想知道&#39; sku&#39;,已经存在于页面中的人,所以我很幸运)

我需要变量在iframe中动态打印它们的值,我将把它放在上面的jQuery中:

<iframe src="https://secure.img-cdn.mediaplex.com/0/24663/universal.html?page_name=sale&Sale=1&Amount={' + revenue + '}&Quantity={' + quantity + '}&mpuid={' + sku + ',' + orderid + '}" HEIGHT=1 WIDTH=1 FRAMEBORDER=0></iframe>

上面我尝试添加变量名称,但用空格和+符号包围,但这不起作用。它只是简单地在浏览器中显示上述内容。

任何想法,人?

1 个答案:

答案 0 :(得分:0)

好的,所以我设法解决了这个问题。这个方法很长,但是我不得不解决几个障碍(因此我不得不复制并移动一些数据)

<!-- order confirmation page VALUE CLICK -->
<venda_block mode=value,<venda_tpxt mode=get,name=track>=orderreceipt><!-- applies this     code block to the order receipt page only -->
<!-- HTML -->
<p id="valueClickSrc"></p><!-- acts as a store for the js to construct and deploy the dynamic src -->
<iframe id="iframe1" src="#" HEIGHT=1 WIDTH=1 FRAMEBORDER=0></iframe> <!-- the actual      tracking iframe which will be populated by js -->
<p id="totalQuantity" style="position:relative; z-index:9999; background:#066; font-size:14px; color:#FFF;"><venda_shopcart mode=getqty></venda></p><!-- serves to 'wrap' the quantity so that it can be selected by id -->
<!-- witch craft -->
<script type="text/javascript">
var totalPrice = jQuery('.veryImportant.ordertotal div').html(); //grabs the order total     price from the receipt and stores it in a variable
jQuery('#buttons').append('<p id="totalPrice" style="display:none;">'+totalPrice+'</p>'); //prints the total price inside a p tag so that it can be selected
var val = jQuery('#totalPrice').html(); //grabs the order total price from the newly printed p tag above
jQuery('#totalPrice').html(val.substring(1, val.length)); //removes the first character from the #totalPrice string (in this case, the unwanted £ GBP pound symbol)
var orderid = jQuery('.ordernumber strong').text(); //order ID number
var revenue = jQuery('#totalPrice').text(); // total order revenue
var quantity = jQuery('#totalQuantity').text(); //quantity per table row/product name
// sku is a variable that appears to already be in side the default checkout page js. I got lucky with that one.
alert(orderid + " " + revenue + " " + sku + " " + quantity); //tests that all variables are printing as they should
jQuery('#valueClickSrc').html('https://secure.img-cdn.mediaplex.com/0/24663/universal.html?page_name=sale&Sale=1&Amount=%7b'+revenue+'%7d&Quantity=%7b'+quantity+'%7d&mpuid='+sku+'id'+ orderid); //writes the dynamic url with a combination of text and variables. This is placed inside the <p> above called #valueClickSrc
var valueClickSrc = jQuery('#valueClickSrc').html(); //stores the content of the <p> above into a variable
jQuery('#iframe1').attr('src', valueClickSrc); //applies the content from #valueClickSrc into the src attribute of the iframe
</script>
</venda_block>
<!-- End Value Click tag -->

对于在Venda平台上工作的任何人,他们可能会觉得这很有用。

如果有人有建议,我会很高兴听到他们的意见。