Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Javascript / Client Side Development

problem with String.replace in console verses web page


Hopefully someone can point out what I'm doing wrong.

I find myself having to dynamically create HTML code, and have found
that the usual way you see to do this is an unreadable mess, like this:

     blah('<span id="' + id + '"><a href="' + link + '">' + linkText +
'</a></span>');

So instead, I would like to do something more like the variable
interpolation in Perl and other languages:

     blah(XXX('<span id="@id"><a href="@link">@linkText</a></span>'));

The XXX function here would interpolate the values of any variables
named with a @ prefix into the string.  Why @?  Why not?

So using my favorite JavaScript console (FireBug), I typed the following
and ran it:

        x = "hello world";
        y = 42;
        z = "--@x/@y--"

        z = z.replace(/@(\w+)/g, function (dummy, v) {
             return eval(v);
         });

        alert(z);

And it works.  The alert box shows "--hello world/42--" as expected.
Okay, so now let's turn it into a function:

        x = "hello world";
        y = 42;

        function XXX(s) {
            return s.replace(/@(\w+)/g, function (dummy, v) {
                return eval(v);
            });
        }

        alert(XXX("!!!@x~~~@y!!!"));

And again, it works.  The alert box shows "!!!hello world~~~42!!!" as
expected.  I should be able now to just drop this *same* code into a
<script> section of a web page, right?

Wrong.  If I take the *same* code as my second example and plop it in a
web page, the alert does not show the same thing as when in FireBug's
console.  It shows the same string passed to XXX, unchanged.

Other experimentation tells me that when placed in a web page, the
anonymous function to do the eval call is never invoked, which suggests
that the regular expression didn't match.

I've only tried this in FireFox 2 and IE 7, but if it doesn't work
properly in either of those, I really don't care about the other browsers.

My guess is there is a difference in the execution environment of
FireBug's console (or likely, any JavaScript console) and the execution
environment of a web page.  The questions are what is that difference,
and how do I make my XXX function work properly?

On May 17, 1:11 am, John Passaniti <put-my-first-name-

h@JapanIsShinto.com> wrote:

<*snip*>

> Okay, so now let's turn it into a function:

>         x = "hello world";
>         y = 42;

>         function XXX(s) {
>             return s.replace(/@(\w+)/g, function (dummy, v) {
>                 return eval(v);
>             });
>         }

>         alert(XXX("!!!@x~~~@y!!!"));

<*snip*>

> I've only tried this in FireFox 2 and IE 7, but if it doesn't work
> properly in either of those, I really don't care about the other browsers.

Can't replicate the failure, works fine in both for me.

---
Geoff

Geoffrey Summerhayes wrote:
> Can't replicate the failure, works fine in both for me.

I'm starting to see the problem.

In my testing, I had tried to run that code inside an existing web
page-- a page that prior to my call brought in jQuery and a handful of
other JavaScript libraries.  When I stripped things down to bare
minimum, it worked.  It's very likely my problem is that one of those
other JavaScript libraries is mucking around with String.replace in some
odd way.

Grrr.

On May 17, 3:05 pm, John Passaniti <n@JapanIsShinto.com> wrote:

> Geoffrey Summerhayes wrote:
> > Can't replicate the failure, works fine in both for me.

> I'm starting to see the problem.

> In my testing, I had tried to run that code inside an existing web
> page-- a page that prior to my call brought in jQuery and a handful of
> other JavaScript libraries.  When I stripped things down to bare
> minimum, it worked.  It's very likely my problem is that one of those
> other JavaScript libraries is mucking around with String.replace in some
> odd way.

> Grrr.

Just as well, my reply to your email wouldn't go through.

Technical details of permanent failure:
PERM_FAILURE: SMTP Error (state 13): 550 <n@japanisshinto.com>:
Recipient address rejected: User unknown in virtual alias table

---
Geoff

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc