views:

43

answers:

2

I'm a noob in Javascript but here is my problem:

I'm cleaning up some PHP-files. Some of them contain Javascript functions which I want to transfer to a separate xxx.js file. Most of them are working fine again but one causes me trouble. I think because of the punctuation (the ' and "). Here's the script as it shows up IN the PHP-file:

function preview(){
  dd=window.open('','prv','height=600,width=500,resizable=1,scrollbars=1')
  document.addnews.mod.value='preview';document.addnews.target='prv'
  document.addnews.submit();dd.focus()
  setTimeout(\"document.addnews.mod.value='addnews';document.addnews.target='_self'\",500)
       }

When copying this to the xxx.js file it won't work. Anybody knows how it should look in a real .js-file?

Thanks in advance!

+3  A: 

Remove the backslashes in front of the double quotes.

setTimeout("document.addnews.mod.value='addnews';document.addnews.target='_self'",500)

It looks like this function was originally in a double-quoted string, so all double-quotes inside are escaped. Good for you for moving them out of PHP :)

Matchu
Darin Dimitrov's answer is better Javascript form, so you should probably use that. Even so, double quotes are what caused the problem, in case you encounter more of them as you continue :)
Matchu
+4  A: 

Cleaned a bit:

function preview() {
    var dd = window.open('', 'prv', 'height=600,width=500,resizable=1,scrollbars=1');
    document.addnews.mod.value = 'preview';
    document.addnews.target='prv';
    document.addnews.submit();
    dd.focus();
    setTimeout(function() {
        document.addnews.mod.value = 'addnews';
        document.addnews.target = '_self';
    }, 500);
}
Darin Dimitrov
This did the trick!Thanks Darin and everybody else who helped that quick!
Dutchie