Help to write a simple script, I do not know whether it is possible using javascript.

If the text in the address contains ?field_number_value and there is a block class="block" on the page, then output the alert "script works."

If about the second part, I can still check, then how to check the address bar, I will not attach my mind.

    3 answers 3

    console.log (window.location);

    Specifically ?field_number_value :

     console.log(location.search) 

    Condition:

     if(location.search.indexOf('?field_number_value')!=-1) 
    • I would know what it is and how it relates to my line - Evgeny Shevtsov
    • Press the f12 button in the browser. there is a console - a very useful thing. Your line? Field_number_value lies in the global location object in the search attribute, where you can check it for the word you need - Darth
    • I need the script to check the address bar for the presence of a word and if there is a word, then output an alert "script works" - Evgeny Shevtsov
    • Added a condition in response - Darth
    • Use the console for debugging, not alerts. It is in every browser, and it helps a lot. - Darth

    Checked in FF works:

      if( ~window.location.search.indexOf('field_number_value') && document.getElementsByClassName('block').length) { alert("Works!"); } else { alert("Doesn't work"); } 
    • one
      Why use !! in if ? He does not care whether bool is there or not. And for && it doesn't matter either. - Qwertiy
    • @Qwertiy really. Updated. - Sergiks

    Will fit?

     if (window.location.pathname.indexOf('field_number_value')!=-1 && $('.block').length) { alert('скрипт работает'); } 
    • for .indexOf() , remember, this beautiful hack is: !!~ instead of !=-1 ? Byte saved - byte earned! ) - Sergiks
    • I have already written several scripts, they all work, but those that need to interact with the address bar - no, Is this a hard condition #indexOf ('field_number_value') # ?? because as you understand, in addition to the address bar, the address of the site and a bunch of other parameters are also written - Evgeny Shevtsov
    • one
      There are no GET request parameters in window.location.pathname . They are in href and search . - user194374