1

I have inserted the below code to display my file in content editor web part.

   <object><embed src="somfile" height="850" width="850" 

   type="application/pdf"></embed></object> 

The content editor is on a separate page. When I go to the page containing content editor, I am also passing a query string with the location of file in the library. Now I want to replace the src in <embed> so that the specified file is opened in the content editor. How can I do that?

1 Answer 1

0

You can do this through javascript. If you pass the querystring in the URL, you can fetch it with javascript and update the embed element.

How to get querystring. Code from answer, below:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

So in your Content Editor Web Part, you place the following javascript above/below (whichever) your embed element.

Example:

<script type="text/javascript">
    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }

    var qsValue = getParameterByName("query string name here");
    if (qsValue)
    {
        var embed = document.getElementById("ExampleEmbed");
        embed.src = qsValue;
    }
</script>

<object><embed id="ExampleEmbed" src="somfile" height="850" width="850" 
   type="application/pdf"></embed></object>
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.