jquery: Adding code around a html-section(with a id) with no direct editing options
I´m try开发者_如何学Pythoning to add some Google Website Optimizer code in front of a section of html and some code after the section. The problem is that I can´t impliment it directly due to that the site is edited in a editor and the options to put in code /edit html is very limited.
I can impliment code/script in the header and on the buttom of the page only.
I want to impliment:
<script>utmx_section("Buybutton")</script>
In front of:
<TABLE ID="BUYSECTION">
<TR>
<TD ALIGN="LEFT" VALIGN="TOP">Amount<BR><INPUT ID="amount" TYPE="TEXT" CLASS="TextInputField_ProductInfo" NAME="AMOUNT" SIZE="3" MAXLENGTH="6" VALUE="1"></TD>
<TD> </TD>
<TD CLASS="BuyButton_ProductInfo">Buy<BR><INPUT TYPE="IMAGE" BORDER="0" SRC="/images/buybutton.png"></TD>
</TR>
</TABLE>
And the following code right after:
</noscript>
I have tried with the follow code (jquery) at the buttom of the page:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
$('<script>utmx_section("Buybutton")</script>').insertBefore('#BUYSECTION');
$('</noscript>').insertAfter('#BUYSECTION');
</script>
But it do not work...
Do anybody see the error ? / see another solution !?
.
UPDATE UPDATE UPDATE:
AlienWebguy helped alot... but its not quite works yet. I tried to fix the code myself from the work AlienWebguy did, but this do not work.
This is what I got so far:
<script>
// Create script tag with native JS
var script = document.createElement('script');
script.type = 'text/javascript';
script.innerHTML = eval('utmx_section("Buybutton")');
// Add it in front of the html-section with JQuery
$(script).insertBefore('#BUYSECTION');
// Add a </noscript>-end-tag after the html-section
$('#BUYSECTION').parent().html($('#BUYSECTION').parent().html() + '</noscript>');
</script>
It do not make any errors in the chrome console - but it do not validate in Google Website Optimizer.
Do anyone see the error ? -or can figure out an easier solution ?
Thanks in advance!
- Anders
Try this:
$(function(){
// Create script tag with native JS
var script = document.createElement('script');
script.type = 'text/javascript';
script.innerHTML = eval('utmx_section("Buybutton")');
// Add it with JQuery
$('head').append(script);
// Wrap table with noscript
$('#BUYSECTION').wrap('<noscript />');
});
EDIT: It wouldn't necessarily immediately follow the table, but this would place the closing noscript tag after the table inside the same container:
$('#BUYSECTION').parent().html($('#BUYSECTION').parent().html() + '</noscript>');
精彩评论