Search code examples
jqueryfancybox

test and strip characters from jquery result for fancybox


$.fancybox.open({
    helpers: {
        title: {
            type: 'inside'
        },
        buttons: {}
                    },
                    padding : 0,
                    href: $('#url').val(),
.......

I would like to check the value of href: and strip http: and https: from the result. I am not really that great with jquery and javascript and even less with something like fancybox.

Suggestions?


Solution

  • You could do a split/slice/join

    $('#url').val().split('://').slice( $('#url').val().split('://').length-1 ).join('')
    

    like this

    $.fancybox.open({
        helpers: {
          title: {
              type: 'inside'
          },
          buttons: {}
                      },
                      padding : 0,
                      href: $('#url').val().split('://').slice( $('#url').val().split('://').length-1 ).join(''),
    .......
    

    Below sample shows how to and make sure it won't break if there is no protocol

    console.log( $('#url').val().split('://').slice( $('#url').val().split('://').length-1 ).join('') )
    
    console.log( $('#url2').val().split('://').slice( $('#url2').val().split('://').length-1 ).join('') )
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <input id="url" value="http://some.data.com">
    <input id="url2" value="some.data.com">