-
-
Save hereswhatidid/8c8edef106ee95138b03 to your computer and use it in GitHub Desktop.
<p>Some HTML goes here</p> | |
<script type="text/javascript" data-keepinline="true"> | |
// this script will remain here when rendered | |
alert( "hello!" ); | |
</script> | |
<script type="text/javascript"> | |
// this script will be forced to the bottom of the page | |
alert( "hello again!" ); | |
</script> |
<?php | |
Class Media extends MediaCore | |
{ | |
public static function deferScript($matches) | |
{ | |
if (!is_array($matches)) | |
return false; | |
$inline = ''; | |
if (isset($matches[0])) | |
$original = trim($matches[0]); | |
if (isset($matches[1])) | |
$inline = trim($matches[1]); | |
/* This is an inline script, add its content to inline scripts stack then remove it from content */ | |
if (!empty($inline) && preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0 && Media::$inline_script[] = $inline) | |
return ''; | |
/* This is an external script, if it already belongs to js_files then remove it from content */ | |
preg_match('/src\s*=\s*["\']?([^"\']*)[^>]/ims', $original, $results); | |
if (isset($results[1]) && (in_array($results[1], Context::getContext()->controller->js_files) || in_array($results[1], Media::$inline_script_src))) | |
return ''; | |
/* return original string because no match was found */ | |
return $original; | |
} | |
} |
Hi,
With regards to the script below, is it necessary? Also how do we know that the script already belongs to js_files? Which file/folder should we refer to?
/* This is an external script, if it already belongs to js_files then remove it from content */ preg_match('/src\s*=\s*["\']?([^"\']*)[^>]/ims', $original, $results); if (array_key_exists(1, $results)) { if (substr($results[1], 0, 2) == '//') { $protocol_link = Tools::getCurrentUrlProtocolPrefix(); $results[1] = $protocol_link.ltrim($results[1], '/'); } if (preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0) { if (in_array($results[1], Context::getContext()->controller->js_files) || in_array($results[1], Media::$inline_script_src)) return ''; } else { Context::getContext()->controller->removeJS($results[1]); } }
In PS 1.6.1.11 it seems a set of empty script tags are created at the end for me.
<script type="text/javascript">
<script>
</script>
I can not seem to find override/classes/Media.php
in my wordpress project.
Is this directory specific to the prestashop theme? As I'm having this issue with the Scalia theme, and can not find where to place this code.
Hi everybody !
I am using prestashop 1.6.1.4. And these codes don't work for me.
The most important for me it's this code :
By
I have overrided the Media class and deleted the index cache but when I add a script like Google Tag Manager or another, it doesn't work.
When I want to call the function I do :
May I have help please !
Thank you so much...