tags:

views:

51

answers:

1

The scenario is that I'd like to update a javascript snippet (that renders a widget) when a user clicks a button.

The idea is that a user can enter a number of parameters to configure their widget and then click a button to see a preview of it.

Is there a way to do this via jquery?

The snippet would look something like this:

<script type="text/javascript"><!--
var yoc_w_frame = "SiteName";
var yoc_home = "HomePath";
var yoc_w_width = 300;
var yoc_w_height = 250;
//--><script>
<script type="text/javascript" src="http://domain.com/js/js.js"&gt;&lt;/script&gt;

I tried $("#divTest").append( script ); where script is a var that holds the built up script string.

+1  A: 

I've managed to to do it like this (some ugly code follows):

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
    </head>
    <body>
        <script type="text/javascript">
         var i = 0;

         function update() {
            i++; 
            var script = '<script type="text/javascript"> function test() { alert(' + i + ');}<\/script>';
            $(script).appendTo(document.body);
            test();
         }
       </script>

       <a href="javascript:update()">click</a>
    </body>
</html>
korchev
Thanks for responding.This does work, however, there is something different about trying to write the script tag with the src attr:<script type="text/javascript" src="http://domain.com/js/js.js"></script>from my example above, this is actually the piece that does the rendering. Any ideas?
lambs
Have you tried $.getScript()?
korchev
I have, and could get the widget to render at the bottom of the page, but didn't have any luck getting it to render by appending it to a div, (for placement). I'll have to read more about the $.getScript() method and how to works.
lambs