Carmen
Carmen

Reputation: 2873

Javascript Replace HTML Characters

I am a total noob on Javascript. What I want is to have a search form, and the input to the form should change the url, then it will open a new tab with the new url.

For example, when I enter 'production', the url will become http://production-test.com/test/, then a new tab will open with the new url.

I know that this should be in Javascript, but I don't know how it will be able to return back the new url.

I have this html so far which does not work at all.

 <script language="javascript">
 function change_url(result) {
     #var result = document.getElementById().value;
     var new_url = "http://" + result + "-test.com/test/';
     # open new window with the new url
 }

 <form id="search-box" name="search_box" method="get" 
action="http://variable-test.com/test/" target="_blank"">

 <input id="search-text" type="text" autocomplete="off" 
class="form-text-box no-focus" name="c" value="" aria-haspopup="true">
 <input type="radio" id="rel" name="sort" value="1" onchange="saveType('rel');" /><span data-message="TypeOp1">Relevance</span><br />

 <input id="search-button" type="submit" />

Upvotes: 0

Views: 292

Answers (2)

Dr.Molle
Dr.Molle

Reputation: 117324

Assuming your final goal is not only to open the new url in a new tab/window and you also want to send the form to the new url in the new tab/window:

You may change the action of the form before submitting the form:

function change_url(form) { 
     var result = document.getElementById('inputId').value;        
     form.action = "http://" + result + "-test.com/test/";
     return true;
 }

and add this to the form-tag:

onsubmit="return change_url(this)"

The form will be sended now to the new url and into a new window/tab (because you set the target to "_blank")

Upvotes: 1

Andreas Wong
Andreas Wong

Reputation: 60516

You can use window.open

window.open(new_url);

If you prefer the window to open in a new tab / window, pass in '_blank' as second argument

window.open(new_url, '_blank');

Upvotes: 0

Related Questions