简体   繁体   中英

Set Browser Title for PDF Page

I load a pdf that the user clicks on via a URL call. See the following javascript:

$("ul.card[data-entry-id]").css("cursor","pointer").on("click",
    function(event)
    {
        document.location = "/archives/entry/" + $(this).attr("data-entry-id");
    }
);

I want to change the browser title of the pdf that comes up, so that it is not just the url. Adding document.title("New Title") to the function block does not work because it is not synchronized with the server returning the file that's being displayed in the browser. How can I overcome this?

Perhaps I could open a new page (rather than changing the document location) that wraps the URL call in html so that I can set the title - something like this:

<html>
    <head>
        <title>New Title</title>
    </head>
    <body>
        <iframe src="/archives/entry/98"></iframe>
    </body>
</html>

And that way, I could set the title. How can I write this html to a new page from within the javascript function block I have that responds to a click? Thanks in advance.

Instead of changing location you can load data by ajax.

$("ul.card[data-entry-id]").css("cursor","pointer").on("click",
    function(event)
    {
        var title = $(this).text();
        var url = "/archives/entry/" + $(this).attr("data-entry-id");
        document.body.innerHTML = "<p>Loading...</p>"; // Or a loading image

        $.ajax(url).done(function(data){
            $(document.body).html(data);
            document.title = title;
        })
    }
);

If you set a title in the PDF document you can make it display as your browser title.

Follow these instructions:

http://www.w3.org/TR/WCAG20-TECHS/PDF18.html

A jQuery approach, relatively easy to modify for plain JavaScript:

$('body').html('<iframe src="/archives/entry/98">');
document.title = "New Title";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM