简体   繁体   中英

Javascript back-link code inside PHP IF condition

I'm using the following Javascript code for (browser) Back-link URL:

<script type="text/javascript">
    history.pushState(null, null, '<?php echo $_SERVER["REQUEST_URI"]; ?>');

    window.addEventListener('popstate', function(event)
    {
        window.location.assign("https://backlink.com");
    });
</script>

And I would like to put it inside:

<?php
if( strpos( $source, 'abc' ) !== false ) {
    Javascript code should go here
}
?>

I tried it in different ways but always got Syntax error. Can somebody help?

Thanks

As noted in the comments doing something like this is generally considered a bad idea but this should accomplish what you are trying to do.

if( strpos( $source, 'abc' ) !== false ) {
    echo '<script type="text/javascript">
    history.pushState(null, null, "' . $_SERVER["REQUEST_URI"] . '"
    window.addEventListener("popstate", function(event)
    {
        window.location.assign("https://backlink.com");
    });
</script>}';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM