Pregunta

I have a working jQuery that Ive found, to dynamically change text over time, with fade effect. I want the words, to be added automatically, from an external link: Words Page.

How can I do this?

My goal is to keep the JS file small/minimal, since I have more code of other jQuery.

Appreciate any guidance!

Check my: enter link description here

HTML

<span class="textbox">Stylish</span>

CSS

span.textbox{
    display: inline;
    float: left;
    font-size: 20px;
    margin-top: 50px;
    margin-left: 50px;
    color: #131313;
    font-weight: bold;
    letter-spacing: 1px;
}

JS

$(document).ready(function() {

   // List your words here:
var words = [
    'Awesome',
    'Special',
    'Sofisticated',
    'Simple',
    'Great',
    'Better',
    'Stronger',
    'Stylish',
    'Flawless',
    'Envied',
    'Strong',
    'Sucessful',
    'Grow',
    'Innovate',
    'Global',
    'Knowledgable',
    'Solid'
    ], i = 0;

setInterval(function(){
    $('.textbox').fadeOut(500, function(){
        $(this).html(words[i=(i+1)%words.length]).fadeIn(500);
    });
}, 5000);

});
¿Fue útil?

Solución

Use jQuery.ajax()

$.ajax('http://www-958.ibm.com/software/data/cognos/manyeyes/datasets/5-adjectives-to-describe-company-2/versions/1.txt').done(function(response, status) {
    // Check for error result (404, 500, etc)
    if (status !== 'error')
    {
        // Split words using ", " as separator to words array.
        var words = response.split(", ");
        // Your code here to change texts. Nothing changed.
        setInterval(function(){
            $('.textbox').fadeOut(500, function(){
                $(this).html(words[i=(i+1)%words.length]).fadeIn(500);
            });
        }, 5000);
    }
});

But you will not be able to get this url due cross-domain error.

Copy the txt to your domain or make a redirect.

Otros consejos

check out https://api.jquery.com/jQuery.get/

eg

var words = {};

$.get( "http://www-958.ibm.com/software/data/cognos/manyeyes/datasets/5-adjectives-to-describe-company-2/versions/1.txt", function( data ) {
    words = data.split(',');
});

data is what is returned from the server -> .split makes array from a string where the bit inside '' is the seperator.

EDIT

If you cannot use $.get due to cross-domain the answer is:

create script on your server (wordslist.php)

use CURL to get the page -> add the content to a variable $wordsList and then do

return $wordsList;

Then do a $.get to the URL 'wordslist.php' instead.

Hi you need to do an ajax call, and then split the data returned into an array, since it's not a valid json string.

try the following code

$(document).ready(function(){
            $.ajax({
                dataType: "html",
                url: "http://www-958.ibm.com/software/data/cognos/manyeyes/datasets/5-adjectives-to-describe-company-2/versions/1.txt",
                success: function(data) {
                    var words = data.split(', ');
                    var i = 0;
                    setInterval(function(){
                        $('.textbox').fadeOut(500, function(){
                            $(this).html(words[i=(i+1)%words.length]).fadeIn(500);
                        });
                    }, 5000);
                },
                error: function(data) {
                    console.log('error occured');
                }
            });
        });

if you are able to modify the txt file to be a valid json array, you can use json or jsonp datatype

Here is my example. You can't use IBM many eyes because they don't allow cross-domain access. However, you can use Bacon Ipsum. I use this often to generate random text for test cases. Here is the pseudo code:

  1. Make AJAX call to a cross-domain resource
  2. On a successful call, load the data into the words list.
  3. Then start your timeout.

Code (jsFiddle)

$(document).ready(function() {
       // List your words here:
    var words = [];
    var baconApi = 'https://baconipsum.com/api/?callback=?';
    var baconSettings = { 
            'type':'meat-and-filler',
              'paras':'1' 
    };

    var baconCallback = function(bacon) {
        if (bacon && bacon.length > 0) {
            // Make word list
            bacon.map(function(meat) {
                words = words.concat(meat.split(/ /));
            });
        }
        var i = 0;
        // Start Timer
        setInterval( function(){
            $('.textbox').fadeOut(500, function(){
                $(this).html(words[i=(i+1)%words.length]).fadeIn(500);
            });
        }, 5000);
    };

    $.getJSON( baconApi, baconSettings, baconCallback);
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top