Reputation: 13
Let me begin by saying that my knowledge of PHP is limited at best, so I am not really sure if what I have tried is correct.
Essentially, I need PHP to run a specific script depending on a variable (language in this case). From what I have found online, this requires the use of a switch statement, but it doesn't seem to run properly. Below is the beginning of the code
<?php
$lang = "en-US";
switch ($lang) {
case "en-US":
<script type="text/javascript">
(function() {
var se = document.createElement('script'); se.type = 'text/javascript'; se.async = true;
se.src = '//storage.googleapis.com/code.rival.com/js/3ca9-4824-43d0-81ec-0c04fb80901b.js';
var done = false;
se.onload = se.onreadystatechange = function() {
if (!done&&(!this.readyState||this.readyState==='loaded'||this.readyState==='complete')) {
done = true;
}
};
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(se, s);
})();
</script>
;
break;
case "de-DE":
<script type="text/javascript">
(function() {
var se = document.createElement('script'); se.type = 'text/javascript'; se.async = true;
se.src = '//storage.googleapis.com/code.rival.com/js/d8d2-9653-427d-bcb6-13e135d6195e.js';
var done = false;
se.onload = se.onreadystatechange = function() {
if (!done&&(!this.readyState||this.readyState==='loaded'||this.readyState==='complete')) {
done = true;
}
};
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(se, s);
})();
</script>
;
break;
There is a fair bit more code, but I didn't want to place a wall of code here. The default at the end uses the same JavaScript as "case "en-US"".
I hope the problem makes sense.
Upvotes: 0
Views: 56
Reputation: 91792
You cannot mix javascript and php like that. If you need to output javascript, you should echo
it or get out of php.
Also, I'm not sure if there are more differences, but if it is just the name of the script, you could get rid of all the duplicate javascript using an array.
For example:
<?php
$lang = "en-US";
// Store all combinations
$languageScripts = [
'en-US' => '3ca9-4824-43d0-81ec-0c04fb80901b.js',
'de-DE' => 'd8d2-9653-427d-bcb6-13e135d6195e.js',
];
// Output the javascript
?>
<script type="text/javascript">
(function() {
var se = document.createElement('script'); se.type = 'text/javascript'; se.async = true;
// Here you add the script you want to run:
se.src = '//storage.googleapis.com/code.rival.com/js/<?php echo $languageScripts[$lang]; ?>';
var done = false;
se.onload = se.onreadystatechange = function() {
if (!done&&(!this.readyState||this.readyState==='loaded'||this.readyState==='complete')) {
done = true;
}
};
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(se, s);
})();
</script>
<?php
// continue with your php script
Upvotes: 1