0

I've got another difficult problem to solve with JavaScript. The string variable comes from outside form input value as you can see below

document.addEventListener('DOMContentLoaded', function() {
var Country = document.getElementById('countr').value;
}, false);

The string address which I need to change is a part of relative href address and will take two different forms.

models/world/some_site.html

or

models/local/some_site.html

The variable Country will be changing as well for example: German, England, France etc.

If I want to replace 'world' or 'local' with Country variable what I need to do is doing something like that

var address = address.split('world').join(Country).split('local').join(Country);

or

var address = address.replace('world', new RegExp(Country, 'g')).replace('local', new RegExp(Country, 'g'));

The result should be look for example like that

models/German/some_site.html

But it doesn't work I don't know why. Any help will be very precious for me.

I discovered that my Country variable don't want to be processed, why? This script not display my variable at all.

document.addEventListener('DOMContentLoaded', function() {
var Country = document.getElementById('cotr').value;
}, false);

document.write(Country);

So this example don't working as well

address.replace(/(local|world)/,Country)

Any help?

My real code looks as below

Indeks.prototype.wyswietl = function(adres)
{
if (typeof adres == 'undefined') adres = 
document.forms[this.id].elements['hasla'].value;
if (typeof this.ramka == 'string')
{
var okno = 
window.open(adres, this.ramka, 'menubar=yes,toolbar=yes,location=yes,directories=no,status=yes,scrollbars=yes,resizable=yes');
if (okno) okno.focus();
else window.location.href = adres;
}
else this.ramka.location.href = 
adres.replace(/(world|local)/,Country);//here I need replacement adres is address
}
1
  • can you give example input and output? Commented Nov 11, 2013 at 8:29

2 Answers 2

1

First of all, join() is used for arrays and leaves commas in the resulting string, so you should use concat() instead. Second, replace() would be a much simpler solution, like so:

var regex = /(world|local)/;
address.replace(regex,Country);

EDIT:

I've tested my code, so the method itself works. The problem in that case must be in your Country variable. Are you sure its scope is global? The way it appears in that of yours function suggests it isn't visible anywhere outside the event listener. Or perhaps you can link the event listener directly to the rest of your code and pass it as a function argument if you consider globals to be evil.

Sign up to request clarification or add additional context in comments.

3 Comments

This example not working because my Country variable don't want to be processed. See my edited asked question.
How to do it I mean link the event listener directly to the rest of the code.
By the looks of it Indeks is supposed to be a constructor object, so the simplest option here appears to be the use of a global.
1

you want to replace the middlepart of text models/local/some_site.html with Counry variable.

you can use split() function to split the string based on /. after getting the splitted strings you can take index[1] value as it contains country value local or world to be replaced.
and then you can use replace() function for replacing the text.

Sample Code as below:

    <script language="javascript">
    var address="models/local/some_site.html";
    var Country="france";//Example
    var finaladdress=address.replace(address.split("/")[1],Country);    
    </script>

Complete Solution :

Code1: Declare your Country variable as global

var Country;//declare the Country outside the function so it becomes global
document.addEventListener('DOMContentLoaded', function() {
Country = document.getElementById('countr').value;
}, false);

Code2: Do replacement here

Indeks.prototype.wyswietl = function(adres)
{
if (typeof adres == 'undefined') adres = 
document.forms[this.id].elements['hasla'].value;
if (typeof this.ramka == 'string')
{
var okno = 
window.open(adres, this.ramka, 'menubar=yes,toolbar=yes,location=yes,directories=no,status=yes,scrollbars=yes,resizable=yes');
if (okno) okno.focus();
else window.location.href = adres;
}
else this.ramka.location.href = 
adres.replace(adres.split("/")[1]),document.getElementById('countr').value); // here I need replacement
}

12 Comments

or a regex for the replacement... like address.replace(/(local|world)/,Country)
but we don't know the middle part right , sometimes it can be other than (local or world).please correct me if i'm wrong
i have edited my answer with section "solution asper your code" please check that and let me know.
address variable means in your code it is 'adres' right? in that case it available for replacement function right?
That's right adres in my code is address appearing later and the var Country = document.getElementById('countr').value; is in the beginning of the code.
|

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.