M
M
midarovrk2015-10-21 14:13:59
JavaScript
midarovrk, 2015-10-21 14:13:59

How to do a domain replacement in a link using JavaScript?

Hello everyone, please help me to remake the script a little.
There is this design:

<div class="comics">
 
 <div class="str"><a href="http://comicsonline.ru/1/batman/001/1.png" rel="shadowbox[comix]"><img src="http://comicsonline.ru/1/batman/001/1.png" title="Страница №1"></a></div>
<div class="str"><a href="http://comicsonline.ru/1/batman/001/2.png" rel="shadowbox[comix]"><img src="http://comicsonline.ru/1/batman/001/2.png" title="Страница №2"></a></div>
<div class="str"><a href="http://comicsonline.ru/1/batman/001/3.png" rel="shadowbox[comix]"><img src="http://comicsonline.ru/1/batman/001/3.png" title="Страница №3"></a></div>
<div class="str"><a href="http://comicsonline.ru/1/batman/001/4.png" rel="shadowbox[comix]"><img src="http://comicsonline.ru/1/batman/001/4.png" title="Страница №4"></a></div>
<div class="str"><a href="http://comicsonline.ru/1/batman/001/5.png" rel="shadowbox[comix]"><img src="http://comicsonline.ru/1/batman/001/5.png" title="Страница №5"></a></div>

</div>

And here is a script:
<script type="text/javascript">
function func(url) {
if(!url)return;
$('.comics a,.comics img').each(function(){
if(!this.getAttribute('orig-url')){
 this.setAttribute('orig-url',(this.src||this.href));
};
if(this.tagName=='IMG'&&this.src.indexOf('comicsonline')!=-1){
 this.src=url+this.getAttribute('orig-url');
}else if(this.href.indexOf('comicsonline')!=-1){
 this.href=url+this.getAttribute('orig-url');
}
});
};
setTimeout(function(){func('http://site.ru/');},10000);
</script>

Script Checks all "img" and "a" in the div block with the class comics for the domain comicsonline.ru and adds to the link http://site.ru/
I.e. links are like this:
http://site.ru/http://comicsonline.ru/1/batman/001/1.png

How to make the script not add but replace one domain with another?
So that in the end the links turned out like this:http://site.ru/1/batman/001/1.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ostin, 2015-10-21
@midarovrk

Instead of:
You need:

this.src=this.getAttribute('orig-url').replace('http://comicsonline.ru/', url);
(c href similar)
Customize replace to suit your needs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question