0

I have a function where I get some data via a rest-call and put it into a div with an id schnittstelle-01.

const request = new XMLHttpRequest();
request.open(
    'GET',
    'https://purl.ulb.tu-darmstadt.de/vp/r000000-2001?ed=pa000008',
);
request.addEventListener('load', function (event) {
    if ((request.status = 200)) {
        document.getElementById('schnittstelle-01').innerHTML =
            request.responseText;
        console.log(request.responseText);
        for (let el of document.querySelectorAll('#schnittstelle-01 * a'))
            el.href = el.href.replace(
                window.location.origin,
                'purl.ulb.tu-darmstadt.de/vp/',
                
            );
    } else {
        console.log(request.status);
    }
});
request.send();

The retrieving part is not what bothers me, but the el.href.replace() part... Let's say my url is test.de the link are supposed to lead to purl.ulb.tu-darmstadt.de/vp/{link-target} at the moment the links after this point to purl.ulb.tu-darmstadt.de/vp/path-to-site-on-test.de/{link-target}

I cannot figure out how to replace both parts. Any hints would be appreciated! I think I'm missing sth. basic here ...

all the best,

2
  • 1
    The else statement will always execute! Commented May 11, 2022 at 5:10
  • whoops! doesn't change my problem though ... will fix this in a second. coffee is coming in Commented May 11, 2022 at 5:15

1 Answer 1

1

you need location.pathname

el.href = el.href.replace(
    location.origin + location.pathname,
    'purl.ulb.tu-darmstadt.de/vp/',
 );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.