If you don’t go into details, why do I need this, then the task is the following: regardless of whether the site is on the local or remote server, save the name of the current file to the variable value without permission.

Here is the code:

var fileName = location.href.split("/").slice(-1); 

// Value at this stage checked - displays, for example, "info.php"

  if (fileName == "#" || fileName == "") {var fileName="index";} // Если страница - начальная else{ //alert(fileName); // позиция 1 fileName = fileName.replace(/\.php$/i,''); alert(fileName); // позиция 2 } 

If we are not on the main page (index.php), then we get into else . I made sure that it works through alert (fileName); in position 1 . However, in position 2 , it no longer works, i.e. there is no window at all. No error messages.

Such code:

 fileName="info.php"; fileName=fileName.replace(/\.php$/i,''); // 1 в 1 с предыдущим кодом alert(fileName); 

I checked on jsfiddle; everything works there.

What can be wrong?

  • really very strange, perhaps some nuances with greed as an option, try to describe the whole line fileName = "info.php"; fileName = fileName.replace (/ ^ (. *?) \. php $ / i, '$ 1'); alert (fileName); - sivik_xes

1 answer 1

You have a variable fileName array and not a string. It is necessary so:

fileName = fileName.toString().replace(/\.php$/i,'');

  • one
    Thank you On your answer, a few hours of research were successfully completed and the code immediately earned. - Bokov Gleb