Forum Moderators: open

Message Too Old, No Replies

Help with JavaScript and JSON.

         

awsoo

2:53 pm on Nov 17, 2019 (gmt 0)

5+ Year Member



Hi,

I need help with a JS and JSON script.

Here are by step by step
1 - I request in XMLHttpRequest server domains
2 - I request in XMLHttpRequest list of link
3- I replace/remove unwanted info in each link (I'm stuck here)
4- Make a Ajax POST request

For 3 i'm using something like


<!DOCTYPE html>
<html>
<body>

<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>

<p id="demo">http://www.site.org/111/info/q4md-fft</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace("http://", "");
document.getElementById("demo").innerHTML = res;

document.getElementById('demo').value = res;
document.write(''+res+''+res2+'');
}
</script>

</body>
</html>


I need in the JS enviroment to edit a object and remove unwanted code (In this case a url)

NickMNS

5:18 pm on Nov 17, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This answers number 3, as for the other points I'm not sure what you want to achieve and there is no code to show what was attempts.

HTML
note: that on the button element the onclick attribute was removed, and an ID was added. It is preferable to use event listeners to onclick attributes as it provides a clear separation between HTML (presentation) and JS (functionality).


<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>
<p id="demo">http://www.site.org/111/info/q4md-fft</p>
<button id="demo-btn">Try it</button>



Javascript:

var button = document.getElementById("demo-btn"); //select the button
button.addEventListener('click', function() { //added event listener to listen for click
var demo = document.getElementById("demo"), //added a var "demo" to reduce typing and make more readable
str = demo.textContent, //change innerHTML to textContent because you only want the text
res = str.replace("http://", ""); //no change here
demo.innerHTML = res;//no change here except using demo variable
//at this point the functions does what it should

//document.getElementById('demo').value = res; //I don't know what this is supposed to do?
//document.write(''+res+''+res2+'');//res2 is a variable that doesn't exist. (same as above)
});


Here is a working demo:
[jsfiddle.net...]