Visualizzazione dei risultati da 1 a 5 su 5

Discussione: Auto-click sul link

  1. #1

    Auto-click sul link

    Ciao a tutti, espongo il mio problema. Ho una pagina web con un elenco di parole ad ognuna delle quali è associato un indirizzo internet. Uno script individua la parola richiesta cercando nell'elenco una stringa di tre lettere. Fin qui ci siamo ma a questo punto lo script, nelle mie intenzioni, dovrebbe aprire il link relativo. Lo script così com'è però non apre il link perchè dopo che sono state digitate le tre lettere bisogna cliccare sulla parola trovata (parzialmente evidenziata) oppure premere invio. Mi piacerebbe invece che il sito internet si aprisse automaticamente subito dopo aver digitato la terza lettera, senza la necessità di ulteriori azioni.
    Secondo voi si può fare?

    codice:
    <html>
    <head>
    <body bgcolor="#BEF781" vlink="#0000FF">
    <center>Digitare le prime tre lettere:</center><br>
    
    <script>
    function searchForText(toFind,frameToSearch){if(!toFind){window.alert('You have not entered any text to search for');return}
    if(frameToSearch.focus){frameToSearch.focus()}
    if(window.find){if(document.layers){var  ifFound=frameToSearch.find(toFind,!1,!1)}else{var  ifFound=frameToSearch.find(toFind,!1,!1,!0,!1,!0,!1)}}else  if(frameToSearch.document.body&&frameToSearch.document.body.createTextRange){if(frameToSearch.document.selection&&frameToSearch.document.selection.type!='None'){var   theRange=frameToSearch.document.selection.createRange();theRange.collapse(!1)}else{var  theRange=frameToSearch.document.body.createTextRange()}
    var ifFound=theRange.findText(toFind);if(ifFound){theRange.select()}}else{alert('Please use your browser\'s search facility')}
    if(!ifFound){alert('Could not find text:\n'+toFind)}};
    </script>
    
    <div align=center>
    <script>
    if ( ( document.body && document.body.createTextRange ) || window.find ) {
    document.write(  '<form action="" onkeyup="if ( this.elements[0].value.length >2  ){searchForText( this.elements[0].value, window );}">'+
    '<input  style="text-align:center" id="tre" autofocus="autofocus" type="text"  onclick="this.value=null" maxlength="3" size="3"> </form>' );
    }
    if (!("autofocus" in document.createElement("input"))) {
    document.getElementById("tre").focus();
    }
    </script>
    </div>
    
    <center><br><br>
    <a href="http://google.com" class="text">GOOGLE</a><br><br>
    <a href="http://youtube.com" class="text">YOUTUBE</a><br><br>
    <a href="http://facebook.com" class="text">FACEBOOK</a><br><br>
    <a href="http://twitter.com" class="text">TWITTER</a><br><br>
    <a href="http://eccetera.com" class="text">ecc. ecc....</a><br><br>
    </center>
    </head>
    </body>
    </html>
    Ultima modifica di Longo; 17-07-2018 a 14:13

  2. #2

    Ripropongo il codice di cui sopra

    Così è un po' più leggibile


    codice:
    <html>
    
    <body bgcolor="#BEF781" vlink="#0000FF">
        <center>Digitare le prime tre lettere:</center><br>
    
        <script>
            function searchForText(toFind, frameToSearch) {
                if (!toFind) {
                    window.alert('You have not entered any text to search for');
                    return
                }
                if (frameToSearch.focus) {
                    frameToSearch.focus()
                }
                if (window.find) {
                    if (document.layers) {
                        var ifFound = frameToSearch.find(toFind, !1, !1)
                    } else {
                        var ifFound = frameToSearch.find(toFind, !1, !1, !0, !1, !0, !1)
                    }
                } else if (frameToSearch.document.body && frameToSearch.document.body.createTextRange) {
                    if (frameToSearch.document.selection && frameToSearch.document.selection.type != 'None') {
                        var theRange = frameToSearch.document.selection.createRange();
                        theRange.collapse(!1)
                    } else {
                        var theRange = frameToSearch.document.body.createTextRange()
                    }
                    var ifFound = theRange.findText(toFind);
                    if (ifFound) {
                        theRange.select()
                    }
                } else {
                    alert('Please use your browser\'s search facility')
                }
                if (!ifFound) {
                    alert('Could not find text:\n' + toFind)
                }
            };
        </script>
    
        <div align=center>
            <script>
                if ((document.body && document.body.createTextRange) || window.find) {
                    document.write('<form action="" onkeyup="if ( this.elements[0].value.length >2  ){searchForText( this.elements[0].value, window );}">' +
                        '<input  style="text-align:center" id="tre" autofocus="autofocus" type="text"  onclick="this.value=null" maxlength="3" size="3"> </form>');
                }
                if (!("autofocus" in document.createElement("input"))) {
                    document.getElementById("tre").focus();
                }
            </script>
        </div>
    
        <center><br><br>
            <a href="http://google.com" class="text">GOOGLE</a><br><br>
            <a href="http://youtube.com" class="text">YOUTUBE</a><br><br>
            <a href="http://facebook.com" class="text">FACEBOOK</a><br><br>
            <a href="http://twitter.com" class="text">TWITTER</a><br><br>
            <a href="http://eccetera.com" class="text">ecc. ecc....</a><br><br>
        </center>
    </body>
    
    </html>

  3. #3
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,131
    Ciao e benvenuto,
    Se il risultato della ricerca e univoco invece di usare document.write per costruire il form prova usando window.open è gli aggiungi il link. Se il risultato non è univoco (dato unico) l’operazione diventa più complicata dovrai valorizzare un array che conterrà il risultato della ricerca e un ciclo for per aprire tante window quanti valori nella array (è un po’ contorto ma non saprei spiegarlo meglio).
    Non mi vengono altre soluzioni.
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  4. #4
    Questa dovrebbe essere un copia funzionante.
    Sono risalito all'elemento del DOM (il tag "a") partendo dalla selezione.
    Ho commentato il codice.

    codice:
    <html>
    
    
    <body bgcolor="#BEF781" vlink="#0000FF">
        <center>Digitare le prime tre lettere:</center><br>
    
    
        <script>
            function searchForText(toFind, frameToSearch) {
                if (!toFind) {
                    window.alert('You have not entered any text to search for');
                    return
                }
                if (frameToSearch.focus) {
                    frameToSearch.focus()
                }
                if (window.find) {
                    if (document.layers) {
                        var ifFound = frameToSearch.find(toFind, !1, !1)
                    } else {
                        var ifFound = frameToSearch.find(toFind, !1, !1, !0, !1, !0, !1)
                    }
    					 //recupero la selezione
    					 range=window.getSelection().getRangeAt(0);
    					 //dalla selezione risalgo al nodo (<a>) che la contiene e redirigo verso l'url
    					 location.href=range.commonAncestorContainer.parentNode.href;
                } else if (frameToSearch.document.body && frameToSearch.document.body.createTextRange) {
                    if (frameToSearch.document.selection && frameToSearch.document.selection.type != 'None') {
                        var theRange = frameToSearch.document.selection.createRange();
                        theRange.collapse(!1)
                    } else {
                        var theRange = frameToSearch.document.body.createTextRange()
                    }
                    var ifFound = theRange.findText(toFind);
                    if (ifFound) {
                        theRange.select();
    						  range=window.getSelection().getRangeAt(0);
    						  location.href=range.commonAncestorContainer.parentNode.href;
                    }
                } else {
                    alert('Please use your browser\'s search facility')
                }
                if (!ifFound) {
                    alert('Could not find text:\n' + toFind)
                }
            };
        </script>
    
    
        <div align=center>
            <script>
                if ((document.body && document.body.createTextRange) || window.find) {
                    document.write('<form action="" onkeyup="if ( this.elements[0].value.length >2  ){searchForText( this.elements[0].value, window );}">' +
                        '<input  style="text-align:center" id="tre" autofocus="autofocus" type="text"  onclick="this.value=null" maxlength="3" size="3"> </form>');
                }
                if (!("autofocus" in document.createElement("input"))) {
                    document.getElementById("tre").focus();
                }
            </script>
        </div>
    
    
        <center><br><br>
            <a href="http://google.com" class="text">GOOGLE</a><br><br>
            <a href="http://youtube.com" class="text">YOUTUBE</a><br><br>
            <a href="http://facebook.com" class="text">FACEBOOK</a><br><br>
            <a href="http://twitter.com" class="text">TWITTER</a><br><br>
            <a href="http://eccetera.com" class="text">ecc. ecc....</a><br><br>
        </center>
    </body>
    
    
    </html>

  5. #5
    Grandioso Luca, funziona alla perfezione
    Grazie amici, grazie ad entrambi!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.