1

I have a problem with my javascript string in php. So I tried to add a string to a variable in switch but I get error :

$blocPub = '<script type="text/javascript">
          var rdads=new String(Math.random()).substring (2, 11);
          document.write('<sc'+'ript type="text/javascript" src="http://test.com?sdsds?bhhj'+rdads+'dsdsdsdsds"></sc'+'ript>');
            </script>';

The error is in document.write. Exist a method to ecranate this string? Please help me!! Thx in advance.

1
  • He mean escape i suppose Commented May 7, 2015 at 10:12

4 Answers 4

1

You need to escape the quotes inside quote.

$blocPub = '<script type="text/javascript">var rdads = new String(Math.random()).substring(2, 11);document.write(\'<script type="text/javascript" src="http://test.com?sdsds?bhhjbf=fdfdfd"></script>\'); </script>';
Sign up to request clarification or add additional context in comments.

1 Comment

There exist a concatenation in src..How to escape this?Thx
0

See if below works. You can either escape strings or alternate between "(double) and '(single) or do both in your case

case 7 :
        $blocPub = '<script type="text/javascript">
                        var rdads=new String(Math.random()).substring (2, 11);
                        document.write("<script type=\"text/javascript\" src=\"http://test.com?sdsds?bhhjbf=fdfdfd\"></script>");
                    </script>';
        break;

Comments

0
$blocPub = '<script type="text/javascript">
                        var rdads=new String(Math.random()).substring (2, 11);
                        document.write("<sc"+"ript type=\"text/javascript\" src="http://test.com?sdsds?bhhjbf=fdfdfd"></sc"+"ript>");
                    </script>';

Try this, i think you have mistake with quotes

1 Comment

There exist a concatenation in src..How to escape this?Thx
0
$blocPub = '<script type="text/javascript">
                        var rdads=new String(Math.random()).substring (2, 11);
                        document.write("<script type="text/javascript" src="http://test.com?sdsds?bhhjbf=fdfdfd"/>");
                    </script>';

2 Comments

There exist a concatenation in src..How to escape this?Thx
what do you want to achive from this variable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.