'A CGI program replaced a variable ^. This "OldSearchWord" should be replaced via js with a modified OldSearchWord, the NewSearchWord

A Query with an OldSearchWord UNITY SOFTWARE leads to a new page where a variable ^ is replaced with the OldSearchWord UNITY SOFTWARE. This works fine.

After the result page is loaded, the OldSearchWord UNITY SOFTWARE should be modified in this way: The white space character should be replaced with a _. The result, the NewSearchWord, should look like UNITY_SOFTWARE.

The Problem: No NewSearchWord is created and looks like the OldSearchWord.

How can the OldSearchWord UNITY SOFTWARE changed after the page has loaded into the NewSearchWord UNITY_SOFTWARE?

My code:

<body  onload="myFunction()">
New search word:  <span id="NewSearchWord"></span>
<script>
let OldSearchWord;
let NewSearchWord;
OldSearchWord = "^";
</script>
<script>
function myFunction() 
{   
NewSearchWord = ( OldSearchWord.replace(/\s+/, '_') );
document.getElementById("NewSearchWord").innerHTML = NewSearchWord;
window.alert(NewSearchWord);
}
</script>
</body>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source