Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    777

    [javascript] prompt e link

    salve a tutti
    questo è il codice
    codice:
    <script type="text/javascript">
    
    function AddToTextarea(TextareaID,TextToAdd,Titolo,TypeText){	
    	if(TypeText !=""){
    	var L = prompt(Titolo,"");
    		if(TypeText != "link"){
    			if (L) {
    			TextToAdd = '<' + TypeText + '>' + L + '</' + TypeText + '>';
    			}
    		}	
    		else{
    		var M = prompt("Inserisci il link","http://");
    			if(L){
    			TextToAdd= '' + L + '';
    			}
    			else{
    			TexToAdd= '' + M + '';
    			}
    		}
    	}
    						
        var MyTextarea = document.getElementById(TextareaID);
        
        if (document.all)
        {
            MyTextarea.focus();
            var MyRange = document.selection.createRange();
            MyRange.colapse;
            MyRange.text = TextToAdd;
        }
       	 else if (MyTextarea.selectionEnd){
            var MyLength = MyTextarea.textLength;
            var StartSelection = MyTextarea.selectionStart;
            var EndSelection = MyTextarea.selectionEnd;
            MyTextarea.value = MyTextarea.value.substring(0, StartSelection) + TextToAdd + MyTextarea.value.substring(EndSelection, MyLength);
        }
        else{
            MyTextarea.value += TextToAdd;
        }
    
    }
    </script>
    allora la funzione dovrebbe permettermi di aprirmi un prompt a seconda se si preme il pulsante per il grassetto o per il corsivo o sottolineato. aperto il prompt io inserisco ciò che voglio venga formattato. se non c'è nulla non scrive nulla. fin qui va tutto bene.
    per il link ho pensato di distinguere aggiungendo un voce alla funzione per cui se typetext è uguale a link il codice dovrà pensare a questo. aprirmi un primo prompt per inserire il titolo del link e un secondo per inserire il link vero e proprio in modo da scrivere il costrutto html <a>
    il problema è questo. se compilo tutti i due prompt funziona. se lascio libero il titolo e compilo il link non mi mette come speravo <a href=\"link\">link</a>

    deve essere sbagliata la logica del if else
    qual'è il problema

  2. #2
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Intanto vedi se ti va bene ques'esempio già pronto…

    codice:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>MDC Example</title>
    <script type="text/javascript">
    	function insertMetachars(sStartTag, sEndTag) {
    		var bDouble = arguments.length > 1, oMsgInput = document.myForm.myTxtArea, nSelStart = oMsgInput.selectionStart, nSelEnd = oMsgInput.selectionEnd, sOldText = oMsgInput.value;
    		oMsgInput.value = sOldText.substring(0, nSelStart) + (bDouble ? sStartTag + sOldText.substring(nSelStart, nSelEnd) + sEndTag : sStartTag) + sOldText.substring(nSelEnd);
    		oMsgInput.setSelectionRange(bDouble || nSelStart === nSelEnd ? nSelStart + sStartTag.length : nSelStart, (bDouble ? nSelEnd : nSelStart) + sStartTag.length);
    		oMsgInput.focus();
    	}
    </script>
    <style type="text/css">
    	.intLink {
    		cursor: pointer;
    		text-decoration: underline;
    		color: #0000ff;
    	}
    </style>
    </head>
    
    <body>
    
    <form name="myForm">
    
    
    [<span class="intLink" onclick="insertMetachars('&lt;strong&gt;','&lt;\/strong&gt;');">Bold</span> | <span class="intLink" onclick="insertMetachars('&lt;em&gt;','&lt;\/em&gt;');">Italic</span> | <span class="intLink" onclick="var newURL=prompt('Enter the full URL for the link');if(newURL){insertMetachars('&lt;a href=\u0022'+newURL+'\u0022&gt;','&lt;\/a&gt;');}else{document.myForm.myTxtArea.focus();}">URL</span> | <span class="intLink" onclick="insertMetachars('\n&lt;code&gt;\n','\n&lt;\/code&gt;\n');">code</span> | <span class="intLink" onclick="insertMetachars(' :-)');">smile</span> | etc. etc.]</p>
    
    
    <textarea rows="10" cols="50" name="myTxtArea">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut facilisis, arcu vitae adipiscing placerat, nisl lectus accumsan nisi, vitae iaculis sem neque vel lectus. Praesent tristique commodo lorem quis fringilla. Sed ac tellus eros. Sed consectetur eleifend felis vitae luctus. Praesent sagittis, est eget bibendum tincidunt, ligula diam tincidunt augue, a fermentum odio velit eget mi. Phasellus mattis, elit id fringilla semper, orci magna cursus ligula, non venenatis lacus augue sit amet dui. Pellentesque lacinia odio id nisi pulvinar commodo tempus at odio. Ut consectetur eros porttitor nunc mollis ultrices. Aenean porttitor, purus sollicitudin viverra auctor, neque erat blandit sapien, sit amet tincidunt massa mi ac nibh. Proin nibh sem, bibendum ut placerat nec, cursus et lacus. Phasellus vel augue turpis. Nunc eu mauris eu leo blandit mollis interdum eget lorem. </textarea></p>
    </form>
    
    </body>
    </html>

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    777
    andrebbe bene però devo gestire alcune cose perciò 4 parametri da poter inserire
    nella mia funzione cosa è che non va nell'if?

  4. #4
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Con l'esempio che ti ho mandato puoi gestire l'etichetta del link... senza il fastidioso prompt. Altrimenti aspetta altri suggerimenti...

  5. #5
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    777
    ti ringrazio per l'aiuto ma preferisco avere il prompt perchè mi sembra semplice per gente che magari è alle prime armi

  6. #6
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    777
    ci sto riuscendo a ottenere lo scopo anche se ho messo settecento if else
    codice:
    if (L != null){
    			var M = prompt("Inserisci il link","http://");
    			if(M != null  && (M != "" || M != "http://")){
    
    				if(L == ""){
    				var L = M;
    				}
    				TextToAdd= '' + L + '';
    				}
    			}
    devo considerare i seguenti casi
    L(primo prompt)
    M(secondo prompt)
    L può essere anche vuoto ma non null, se null non si fa niente
    se L non è nullo, M non deve essere nullo o vuoto e tanto meno contenere http://
    se L è vuoto M è UGUALE A L

    questo è il punto in cui sono.

  7. #7
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    777
    allora credo di aver risolto
    codice:
    if (L != null){
    			var M = prompt("Inserisci il link","http://");
    				if(M != null){
    				if (M == "http://"){
    				var M = "";
    				}
    					if(M != ""){
    						if(L == ""){
    						var L = M;
    						}
    					TextToAdd= '' + L + '';
    					}
    				}
    			}	
    			}
    non è il massimo della bellezza ma funziona considerato il fatto che sono un principiante di javascript

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.