javascript, jQuery

SquareSpace Scroll + YouTube PopUP

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

<script type="text/javascript">
	$(document).ready(function() { 
    
    $('a[href*="https://www.youtube.com/watch?"]').each(function() {
  $(this).attr('data-fancybox','');
 });
      
   $('a[href*="https://youtu.be/"]').each(function() {
  $(this).attr('data-fancybox','');
 });    
	
	
	});
</script>

<script>
$(document).ready(function(){
  // Add smooth scrolling to all links
  $("a").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){
   
        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});
</script>

Aggiungendo il codice in tutte le pagine non ci sono problemi

il clic si attiva su ID dell’elemento sia un <p> o un <div>

 

javascript

mobile da javascript

<script>
jQuery( document ).ready(function($) {      
    var isMobile = window.matchMedia("only screen and (max-width: 760px)").matches;

    if (isMobile) {
        //Conditional script here

    } 
 });

</script>

 

javascript, jQuery, PHP, WordPress

Woocommerce Checkout – Configurazione campi

	function newFieldCheck(){
		
		//controlliamo di essere nella pagina giusta

		if (is_checkout()) {
	
		?>

			<script type="text/javascript">
				
			jQuery( document ).ready(function( $ ) {  
				
			// nascondo i campi di fatturazione
			
			$('#ragione_sociale_field').hide();
			$('#partita_iva_field').hide();
			$('#codice_destinatario_field').hide();
			$('#pec_field').hide();

				
			 // imposto un trigger sul campo di richiesta di fatturazione
			 	
			  $('#fattura_ricevuta').on('change', function (e) {
				  
// 				    var optionSelected = $("option:selected", this);
				    var valueSelected = this.value;
				    
				    if (valueSelected == 'SI'){
					    
					    $('.optional').hide();  //rimuovo la scritta opzionale sui campi di fatturazione
					    
					    	$('#ragione_sociale_field').show();
							$('#partita_iva_field').show();
							$('#codice_destinatario_field').show();
							$('#pec_field').show();		    			 
				    } 
				     
				     if (valueSelected == 'NO') {
					    
					    	$('#ragione_sociale_field').hide();
							$('#partita_iva_field').hide();
							$('#codice_destinatario_field').hide();
							$('#pec_field').hide();		    		    
				    }

				  
			  });				
				
			});  //end ready


			</script>
	
	
		<?php
			
		}
		
		
	} //end cpCheck



add_action('woocommerce_before_checkout_form', 'newFieldCheck');

 

 

 function validazioneFattura() {

		    $fattura = filter_input(INPUT_POST, 'fattura_ricevuta');
		    $ragione_sociale = filter_input(INPUT_POST, 'ragione_sociale');
   		    $piva = filter_input(INPUT_POST, 'partita_iva');
   		    $sdi = filter_input(INPUT_POST, 'codice_destinatario');
   		    $pec = filter_input(INPUT_POST, 'pec');
   		    $codice_fiscale = filter_input(INPUT_POST, 'codice_fiscale');

   			
   			if (strlen($codice_fiscale) != 16) {
		        wc_add_notice(__('<strong>Codice Fiscale:</strong> Errore, controlla i dati inseriti.'), 'error');
		    }

		
		    if ($fattura == 'SI' AND $ragione_sociale == '') {
		        wc_add_notice(__('<strong>Errore</strong>, devi inserire una Ragione Sociale'), 'error');
		    }
		    
		    // check partita iva  30/04/2019 - 16:30
		    
			if (($fattura == 'SI' AND strlen($piva) != 11) or ($fattura == 'Fattura' AND $piva == ''))  {
		        wc_add_notice(__('<strong>Partita IVA</strong>, Errore, controlla i dati inseriti'), 'error');
		    }
		    
		    // controllo fatturazione elettronica 24/02/2019 - 13:32
		    
		    if ($fattura == 'SI' AND $pec == '' AND $sdi == ''){
		        wc_add_notice(__('<strong>Errore</strong>, devi inserire un Codice Destinatario o un indirizzo PEC'), 'error');
		    }
			   
		    
		}
		
		add_action('woocommerce_checkout_process', 'validazioneFattura');

 

javascript, WordPress

Link WebApp stessa pagina

Quando si crea una web app, i link tendono ad aprirsi in una nuova finestra del browser, questo non deve succedere

<script type="text/javascript">
// fix link open new windows WebApp

(function(a,b,c){if(c in b&&b){var d,e=a.location,f=/^(a|html)$/i;a.addEventListener("click",function(a){d=a.target;while(!f.test(d.nodeName))d=d.parentNode;"href"in d&&(d.href.indexOf("http")||~d.href.indexOf(e.host))&&(a.preventDefault(),e.href=d.href)},!1)}})(document,window.navigator,"standalone")
	
</script>

Oppure in maniera selettiva escludendo singoli elementi (utile per gallerie immagini)

<script type="text/javascript">
jQuery('a:not(".asg-lightbox"):not([href^="#"])').live('click', function (event)
{      
    var href = jQuery(this).attr("href");

    if (href.indexOf(location.hostname) > -1)
    {
        event.preventDefault();
        window.location = href;
    }
});
</script>

 

javascript, jQuery

Smooth Scroll

<script type="text/javascript">
 jQuery(document).ready(function($) { 
 
 //EFFETTO MORBIDOSO
 
 $(function() {
 $('a[href*="#"]:not([href="#"])').click(function() {
 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
 var target = $(this.hash);
 target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
 if (target.length) {
 $('html, body').animate({
 scrollTop: target.offset().top-100
 }, 1000);
 return false;
 }
}
 });
});
 
 });
</script>
javascript, jQuery

url in javascript

http://www.test.com:8082/index.php#tab2?foo=123

window.location.host                   www.test.com:8082
window.location.hostname               www.test.com
window.location.port                   8082
window.location.protocol               http
window.location.pathname               index.php
window.location.href                   http://www.test.com:8082/index.php#tab2
window.location.hash                   #tab2
window.location.search                 ?foo=123

jQuery

$(location).attr('host');                        www.test.com:8082
$(location).attr('hostname');                    www.test.com
$(location).attr('port');                        8082
$(location).attr('protocol');                    http
$(location).attr('pathname');                    index.php
$(location).attr('href');                        http://www.test.com:8082/index.php#tab2
$(location).attr('hash');                       #tab2
$(location).attr('search');                     ?foo=123