Jump to content

Parse Error when inserting code


Jimmy058910

Recommended Posts

Hi,

 

I tried putting this Viglink code into my custom themes index.template.php on my SMF forum (I should do that right?) and I ended up with this error...

 

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in .../Themes/androidrtk_rev4n/index.template.php on line 554

545: 				subMenusContainerId:\'admsubMenus\',
546: 				duration:\'150\'
547: 				});
548: 		});
549: 	}
550: 	</script>
551: 
552: <script type="text/javascript">
553:   var vglnk = { api_url: '//api.viglink.com/api',

554:                 key: 'ad93febdf4e33c034d9635037b687fb3' };

555: 
556:   (function(d, t) {
557:     var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;
558:     s.src = ('https:' == document.location.protocol ? vglnk.api_url :

 

I figured since it was a copy paste code that it would work just fine  :-\

 

In any case, how can I fix this?

 

Thanks in advance,

Jimmy

Link to comment
https://forums.phpfreaks.com/topic/233771-parse-error-when-inserting-code/
Share on other sites

The first few lines of the code you posted have the single quites escaped:

 

545: 				subMenusContainerId:\'admsubMenus\',
546: 				duration:\'150\'

 

Which might be because the JavaScript is inside a PHP string. If you escape the other single quotes like this:

 

				subMenusContainerId:\'admsubMenus\',
				duration:\'150\'
				});
		});
	}
	</script>

<script type="text/javascript">
   var vglnk = { api_url: \'//api.viglink.com/api\',

                 key: \'ad93febdf4e33c034d9635037b687fb3\' };


   (function(d, t) {
     var s = d.createElement(t); s.type =\ 'text/javascript\'; s.async = true;
     s.src = (\'https:\' == document.location.protocol ? vglnk.api_url :

 

Dose it fix the error?

It doesn't fix the error, but it changes it to this:

 

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in .../Themes/androidrtk_rev4n/index.template.php on line 556

547:  				});
548:  		});
549:  	}
550:  	</script>
551:  
552:  <script type="text/javascript">
553:    var vglnk = { api_url: \'//api.viglink.com/api\',
554:                  key: \'ad93febdf4e33c034d9635037b687fb3\' }; 
555:    (function(d, t) {
556:      var s = d.createElement(t); s.type =\ 'text/javascript'; s.async = true;
557:      s.src = (\'https:\' == document.location.protocol ? vglnk.api_url :
558:              '//cdn.viglink.com/api') + '/vglnk.js';
559:     var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r);
560:   }(document, 'script'));

556:      var s = d.createElement(t); s.type =\ 'text/javascript'; s.async = true;

 

The single quote after javascript needs to be escaped as well as the others. Try:

 

556:      var s = d.createElement(t); s.type =\ 'text/javascript\'; s.async = true;
557:      s.src = (\'https:\' == document.location.protocol ? vglnk.api_url :
558:              \'//cdn.viglink.com/api\') + \'/vglnk.js\';
559:     var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r);
560:   }(document, \'script\'));

whoops there was a space between the backslash and single quote near text/javascript.

 

Try:

 

556:      var s = d.createElement(t); s.type = \'text/javascript\'; s.async = true;
557:      s.src = (\'https:\' == document.location.protocol ? vglnk.api_url :
558:              \'//cdn.viglink.com/api\') + \'/vglnk.js\';
559:     var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r);
560:   }(document, \'script\'));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.