1

I just have write simple script which one change alts in img tag. But it is not working. Here is my script:

jQuery(document).ready(function($) {
    var slowakluczowe = ['alt1', 'alt2', 'alt3'];
    $("img").attr("alt", function() {
        return slowakluczowe[Math.floor(Math.random() * slowakluczowe.length)];
    });
});

The script takes random alt and pins it in to image but this doesn't work well. It doesn't change the alt when I click 'View Source' it only works when I click 'Inspect Element'.

See here: http://prntscr.com/boo9jk

It's working only with 'Inspect Element' How can I make it also work with 'View Source'?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 4
    The content of 'View source' never gets updated - it is the source of the page as it was downloaded from the server. You can only see live updates to the DOM in the DOM Inspector tool. Your code is working absolutely fine. – Rory McCrossan Jul 04 '16 at 14:50
  • 2
    See here - http://stackoverflow.com/questions/9654737/content-of-html-page-changed-by-jquery-but-view-source-dont-reflect-the-chang – dmoo Jul 04 '16 at 14:51
  • 1
    I totally don't understand why you need the updated code to work in VIEW SOURCE –  Jul 04 '16 at 14:51

1 Answers1

2

View source always displays the original data that was received from the server. This cannot be changed with JavaScript. The browser inspector shows a live representation of the DOM, so it will be updated by the JavaScript.

The only way to change the data when you click 'view source' is to send some data to the server, make the changes there, and then refresh the page by whatever method. I can't see why you would ever need to do that though. Perhaps what you actually want to do is to let the user edit content, that will then be viewable by all. In this case you need to send the information to the server, store in a database, and then use this information when presenting the page to users.

The inspector is the more powerful tool and it is the one that you should be using. View source is only really useful when you specifically want to see the data in its unadulterated form, i.e. before any JavaScript on the page has run.

Jimbali
  • 2,065
  • 1
  • 20
  • 24