ISLAMABAD (Reuters) - Pakistan's Supreme Court on Friday struck down a law designed to protect the new prime minister from being charged with contempt and thrown out of office like his predecessor.

The ruling was the latest twist in a long-running saga that pits the country's judiciary against its ruling party, a showdown that many analysts say endangers Pakistan's fragile democracy.

The court ousted the previous prime minister, Yousuf Raza Gilani, from office in June by charging him with contempt of court for failing to reopen corruption proceedings against President Asif Zardari.

The court gave the new prime minister, Raja Ashraf, until August 8, to respond to the court's demand that he write a letter reopening the corruption case.

In response, the government hastily passed a law declaring high-ranking officials immune from contempt of court proceedings.

But the court ruled that the law was unconstitutional because it eroded the court's authority.

(Reporting by Katharine Houreld; Editing by Ed Lane)