Home > OS >  Replace image url of a page just using javascript
Replace image url of a page just using javascript

Time:12-17

I have been trying to find a solution of this simple problem but endup asking here as nothing I tried actually worked.

There is some image link on my site like this =

<img src="https://wordpress.creativegigs.net/docly/wp-content/themes/docly/assets/img/home_one/b_man_two.png"  alt="Man illustration" style="visibility: visible; animation-name: fadeInRight;">

This came with wordpress theme, but looks like they moved the link so this is going to error page, that directory image doesn't exist.

So I want a js code that can replace this link of image src like this = "/assets/img/home_one/b_man_two.png"

I want a js code that can remove this much = https://wordpress.creativegigs.net/docly/ from the src url where ever it exist.

Can anyone please help me with this?

CodePudding user response:

You can try this:

<?php

function replace_image_url_script() {
    ?>
    <script>
        document.addEventListener("load", function(){
            var html = document.documentElement.innerHTML;
            const regex = /https:\/\/wordpress\.creativegigs\.net\/docly/ig;
            html = html.replaceAll(regex, '');
            document.querySelector('html').innerHTML = html;
        });
    </script>
    <?php
}
add_action( 'wp_footer', 'replace_image_url_script' );

CodePudding user response:

## If Using JQuery
$('.p_absolute').attr('src',yourImageURL) ;

## If Normal JS
document.querySelector('.p_absolute').setAttribute('src','yourImageURL');
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
<img src="https://wordpress.creativegigs.net/docly/wp-content/themes/docly/assets/img/home_one/b_man_two.png"  alt="Man illustration" style="visibility: visible; animation-name: fadeInRight;"/>
</body>
</html>

## If Using JQuery
$('.p_absolute').attr('src',yourImageURL) ;

## If Normal JS
document.querySelector('.p_absolute').setAttribute('src',yourImageURL);
  • Related